/* selectchild.c - Using select to communicate with two children. */ #include #include #include #include #include #define MAXLINE 12 int main(void) { int m, n; int fd1[2]; /* pipe for communications from child1 to parent */ int fd2[2]; /* pipe for communications from child2 to parent */ pid_t pid; char line[MAXLINE]; fd_set readset; if (pipe(fd1) < 0) { perror("pipe1"); exit(1);} if (pipe(fd2) < 0) { perror("pipe2"); exit(1);} /* child1 */ if ( (pid = fork()) < 0) { perror("fork1"); exit(1);} else if (pid == 0) { close(fd1[0]); while (1) { write(fd1[1], "from child1\n", MAXLINE); sleep(1);} exit(0);} /* child2 */ if ( (pid = fork()) < 0) { perror("fork2"); exit(1);} else if (pid == 0) { close(fd2[0]); while (1) { write(fd2[1], "from child2\n", MAXLINE); sleep(1.3);} exit(0);} /* parent */ close(fd1[1]); close(fd2[1]); m = 1 + ((fd1[0]