#include #include #include #include #include #include #include int main () { int fd, nfds; fd_set readfds; ssize_t nbytes; char buf[5]; if (mkfifo ("/tmp/myfifo", S_IRUSR | S_IWUSR | S_IWGRP) < 0 && errno != EEXIST) { perror ("mkfifo"); exit (-1); } if ((fd = open ("/tmp/myfifo", O_RDONLY)) < 0) { perror ("open"); exit (-1); } FD_ZERO (&readfds); FD_SET (fd, &readfds); if ((nfds = select (fd + 1, &readfds, NULL, NULL, NULL)) < 0) { perror ("select"); exit (-1); } if (FD_ISSET (fd, &readfds)) printf ("/tmp/myfifo is ready for reading\n"); else printf ("something's wrong\n"); nbytes = read (fd, buf, 4); if (nbytes != 4) { perror ("read"); exit (-1); } buf[4] = '\0'; printf ("read %d bytes: %s\n", nbytes, buf); if (close (fd) < 0) { perror ("close"); exit (-1); } }