gpt4 book ai didi

c - 从管道读取错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:55:54 24 4
gpt4 key购买 nike

void turtle (int gtot)
{
int msg;
fcntl(gtot,F_SETFL,O_NONBLOCK);
read(gtot,&msg,4);

gotoxy(12, 21); printf("The value of buffer for turtle is %d",msg);

//react to god's message
xcoor += msg;
msg = 0;
sleep(sleep_time);
}

void god (int gtot )
{
char choice, sign;
int distance;
scanf("%c",&choice);
//choice = getchar();
gotoxy(1,18);
printf("Enter the distance which should be moved");
fflush(stdout);
gotoxy(50, 14);
scanf ("%d", &distance);
int newd = distance;
//printf ("The distance that is to be moved by %d", distance);

if (choice == 'h')
{
write(gtoh,&distance,4);
}
else if (choice == 't')
{
write(gtot,&newd,4);
gotoxy(12,23);
printf("I am writing %d as the number", distance);
fflush(stdout);
}
//printf("You chose %s", &choice);
sleep(sleep_time);
}
main(){
int gtot[2];
pipe (gtot);
pid_turtle = fork ();
if (pid_turtle == 0)
{
close (gtot[1]);
turtle (gtot[0]);
}

pid_god = fork ();
if (pid_god == 0)
{
close (gtot[0]);
god (gtot[1]);
}
}

当我从 God 函数的管道写入 Turtle 函数时。我希望它在用户不提供任何输入时不返回任何内容,而在用户提供任何输入时返回数字。但是 printf 语句正在打印类似

的输出
       The value of buffer for turtle is 0106368
The value of buffer for turtle is 05291328

这在我看来像是数字的内存地址。我在程序中犯了什么错误。

最佳答案

关于您的程序的几点观察:

在函数 turtle 中:

  1. 您没有初始化变量 msg
  2. 您为 O_NONBLOCK 设置了 gtot 文件描述符。
  3. 但是您不检查read 的返回值。

这是一个严重的问题。 read 立即返回,您正在打印 msg 的未初始化值。

  • forkclose 的方式也在做出贡献。在 fork 之前,您已经关闭了 gtot[1] -ing the "god"process。如果您选择使用这种单父双子进程方法,请不要关闭文件句柄,直到您完成fork-ing。

  • 此外,您似乎打算至少让 turtle 函数,可能还有 god 函数循环。正如所写,您的 turtle 函数将立即退出:它没有循环,并且执行 read 并设置了 O_NONBLOCK 标志。

  • 等等,还有更多。当您确实调用 fcntl 来设置 O_NONBLOCK 时,您所做的也是重置除 O_NONBLOCK 之外的所有标志。这是取自 the libc documentation 的函数它在设置或重置非阻塞标志时处理其他标志:

/* 如果值为非零,则设置 desc 的 O_NONBLOCK 标志, 或者如果值为 0 则清除标志。 成功返回 0,或设置了 errno 的错误返回 -1。 */

int
set_nonblock_flag (int desc, int value)
{
int oldflags = fcntl (desc, F_GETFL, 0);
/* If reading the flags failed, return error indication now. */
if (oldflags == -1)
return -1;
/* Set just the flag we want to set. */
if (value != 0)
oldflags |= O_NONBLOCK;
else
oldflags &= ~O_NONBLOCK;
/* Store modified flag word in the descriptor. */
return fcntl (desc, F_SETFL, oldflags);
}

还有一些其他因素也可能导致您的问题:

  • 无论您在哪里调用 read,最好用 sizeof(int) 替换所有出现的“4”写。整数在您的机器上可能是 8 个字节(64 位整数?),尽管可能性很小。以后int很有可能是8个字节,你的代码在这方面很脆弱。

我观察到您的程序还有一些“奇怪”的地方:

  • 为什么 printf 的十进制输出带有前导 0(零)前缀?那将是一个八进制表示,但 printf 说明符不是 "%o""%d" 不应显示前导零,除非您使用带有前导零的宽度说明符,如 "%08d"。所以我不知道该怎么办。

关于c - 从管道读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3452566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com