select failing with C program but not shell

Posted by Gary on Stack Overflow See other posts from Stack Overflow or by Gary
Published on 2010-05-03T09:44:30Z Indexed on 2010/05/03 9:48 UTC
Read the original article Hit count: 271

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about c

    Related posts about select