i constructed the following paradiam:
parent child
--------------------- --------------------
| | | |
------>| stdin fd[1] | --------> | stdin (fd[0]) |
| | | |
<------| stdout fd[0] | <-------- | stdout (fd[1]) |
| | | |
--------------------- ---------------------
the parent looks like
int main ()
{
... ...
setvbuf (stdout, NULL, _IONBF, 0);
... ...
switch (fork ())
{
... ...
default:
flags = fcntl (fd[0], F_GETFL, 0)))
... ...
flags |= O_NONBLOCK;
flags = fcntl (file_desc[0], F_SETFL, flags)))
... ...
num_byte = read (fd[0], buf, BUF_SIZE);
... ...
write (fileno(stdout), buf, num_byte);
... ...
}
... ...
return 0;
}
the child looks like
int main ()
{
... ...
printf ("hello, world\n");
fflush (stdout);
... ...
sleep (30);
return 0;
}
if i leave out fflush() in the child, "hello, world" never shows up on the console. but if i run the child as a stand-alone app, "hello, world" always shows up with or without fflush(), before the child returns.
why is this?