select failing with C program but not shell
I have a parent and child process, and the parent can read output from the child and send to the input of the child. So far, everything has been working fine with shell scripts, testing commands which input and output data. I just tested with a simple C program and couldn't get it to work. Here's the C program:
#include <stdio.h>
int main( void ) {
char stuff[80];
printf("Enter some stuff:\n");
scanf("%s", stuff);
return 0;
}
The problem with with the C program is that my select fails to read from the child fd and hence the program cannot finish. Here's the bit that does the select..
//wait till child is ready
fd_set set;
struct timeval timeout;
FD_ZERO( &set ); // initialize fd set
FD_SET( PARENT_READ, &set ); // add child in to set
timeout.tv_sec = 3;
timeout.tv_usec = 0;
int r = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
if( r < 1 ) { // we didn't get any input
exit(1);
}
Does anyone have any idea why this would happen with the C program and not a shell one?
Edit: I should point out that the child proc开发者_StackOverflowess calls exec on an argument, so I don't have access to it past that point.
The problem is that the child process is fully buffering the output because it is writing to a pipe and not a terminal. Use setvbuf
to force line buffering or unbuffered mode, or add an explicit fflush
to the child after then printf
, or use the unbuffer program that comes with Expect to trick the child process's libc into thinking the program is working interactively.
Flush your stdout before blocking from input in the C program.
fflush(stdout);
In shell, this is frequently implicit.
Alternately, you can use setvbuf()
to disable buffering (or enable line buffering). This must be called before any data is written:
setvbuf(stdout, NULL, _IONBF, 0)
精彩评论