select failing with C program but not shell
- by Gary
So 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?
Thanks!