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]);
}
}
When I write from the pipe from God function to the Turtle function. I expect it to return nothing when the user gives no input and the numbers when the user gives any. But the printf statement is printing outputs like
The value of buffer for turtle is 0106368
The value of buffer for turtle is 05291328
Which seems to me like the memory address of the number. What is the error that I am making in the program.
Several observations about your program:
In function
turtle:msg.gtotfile descriptor forO_NONBLOCK.read.This is a significant problem.
readis returning immediately and you are printing the uninitialized value ofmsg.The way you
forkandcloseis also contributing. You have closedgtot[1]prior tofork-ing the “god” process. If you choose to use this one-parent of two-child-processes approach, don’tclosethe file handles until you are donefork-ing.Also, it appears that you intended for at least the
turtlefunction, and probably thegodfunction, to loop. As written, your turtle function will immediately exit: it has no loop and it performsreadwith theO_NONBLOCKflag set.But wait, there is more. When you do call
fcntlto setO_NONBLOCK, what you are doing is also resetting every flag other thanO_NONBLOCK. Here is a function taken from the libc documentation which handles the other flags while setting or resetting the non-blocking flag:/* Set the O_NONBLOCK flag of desc if value is nonzero,
or clear the flag if value is 0.
Return 0 on success, or -1 on error with errno set. */
There are some other things which could also be contributing to your problem:
4” withsizeof(int)wherever you are callingreadandwrite. It is possible that integers are 8 bytes on your machine (64 bit integer?), although pretty unlikely. In the future, it is more likely thatintwould be 8 bytes, and your code is very fragile in this regard.There is also something “Strange” about your program that I am observing:
printfbeing prefixed with a leading0(zero)? That would be an octal representation, but theprintfspecifier is not"%o"."%d"should not show leading zeros unless you use a width-specifier with a leading zero, a la"%08d". So I don’t know what to make about that.