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]);
}
}
当我从管道从神功能写入乌龟功能。我希望它在用户没有提供任何输入时不会返回任何内容,而当用户给出时则不会返回任何内容。但printf语句是打印输出像从管道读取错误
The value of buffer for turtle is 0106368
The value of buffer for turtle is 05291328
在我看来,这似乎是数字的内存地址。我在程序中犯的错误是什么?
谢谢,初始化味精工作奇迹! – w2lame 2010-08-10 19:43:00
很高兴听到它。合十礼。 – 2010-08-10 19:43:51
并再次感谢您提供的其他建议。他们真的帮助了我的计划,也帮助了我。 :D – w2lame 2010-08-10 19:58:41