#include #include #include #include #include #include #include #include #define FIFONAME "/tmp/pipe" int main(void) { int fd; int nsel; fd_set readfds; FD_ZERO(&readfds); if (mkfifo(FIFONAME, 0600) < 0) { perror("mkfifo"); exit(1); } fd = open(FIFONAME, O_RDONLY | O_NONBLOCK); if (fd < 0) { perror("open"); remove(FIFONAME); exit(2); } FD_SET(fd, &readfds); do { nsel = select(fd + 1, &readfds, 0, 0, 0); } while (nsel == -1 && (errno == EINTR || errno == EAGAIN)); if (nsel == -1) { perror("select"); exit(3); } if (FD_ISSET(fd, &readfds)) { char buf[100]; int status; while ((status = read(fd, buf, sizeof(buf) - 1))) { if (status > 0) { buf[status] = '\0'; printf("%s", buf); } else { printf("\n"); if (errno != EAGAIN) perror("read"); break; } } } close(fd); remove(FIFONAME); return 0; }