#include #include #include #include #include #include #include #define FIFO_PATH "/tmp/myfifo" int main () { int fd, tmpfd, nsel; fd_set readfds; struct timeval wait_tm = { 0l, 200000l }; /* 200 millisecs */ if (unlink (FIFO_PATH) < 0 && errno != ENOENT) { perror ("unlink"); exit (1); } if (mkfifo (FIFO_PATH, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) { perror ("mkfifo"); exit (1); } printf ("Opening a FIFO for reading with O_NONBLOCK\n"); if ((fd = open (FIFO_PATH, O_RDONLY | O_NONBLOCK)) < 0) { perror ("open"); exit (1); } printf ("Testing for read ready...\n"); FD_ZERO (&readfds); FD_SET (fd, &readfds); nsel = select (fd + 1, &readfds, NULL, NULL, &wait_tm); printf (" select returned %d\n", nsel); printf ("Opening and closing FIFO for writing...\n"); if ((tmpfd = open (FIFO_PATH, O_WRONLY)) < 0) { perror ("open"); exit (1); } if (close (tmpfd) < 0) { perror ("close"); exit (1); } FD_ZERO (&readfds); FD_SET (fd, &readfds); nsel = select (fd + 1, &readfds, NULL, NULL, &wait_tm); printf (" now select returned %d\n", nsel); }