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