#include #include #include #include #include #include #include #include #include #define FIFO "fifo" int main() { int fd; int pid; int flags; fd_set set; struct timeval tv; assert(unlink(FIFO) == 0 || errno == ENOENT); assert(mkfifo(FIFO, DEFFILEMODE) == 0); signal(SIGCHLD, SIG_IGN); pid = fork(); if (pid == 0) { char c; int ret; fd = open(FIFO, O_WRONLY); flags = fcntl(fd, F_GETFL); assert(flags != -1); flags |= O_NONBLOCK; assert(fcntl(fd, F_SETFL, flags) == 0); /* fill the pipe */ for (;;) { c = 'a'; ret = write(fd, &c, 1); if (ret == -1 && errno == EAGAIN) break; } FD_ZERO(&set); FD_SET(fd, &set); tv.tv_sec = 4; tv.tv_usec = 0; ret = select(fd+1, NULL, &set, NULL, &tv); if (ret == 0) printf("child: select() timeout\n"); else printf("child: number writeable of files: %d\n", ret); exit(0); } else { fd = open(FIFO, O_RDONLY); sleep(2); printf("parent: exit\n"); } return (0); }