#include #include #include #include #include #include #include #define FIFOT_NAME "fifot.fifo" int main(int argc, char *argv[]) { int readfd, val, writefd; struct stat stat_buf; if (stat(FIFOT_NAME, &stat_buf) == -1) { if (errno == ENOENT) { if (mkfifo(FIFOT_NAME, S_IRUSR | S_IWUSR) == -1) { (void)fprintf(stderr, "mkfifo() error : %s\n", strerror(errno)); exit(1); } } else { (void)fprintf(stderr, "stat() error : %s\n", strerror(errno)); exit(1); } } if ((readfd = open(FIFOT_NAME, O_RDONLY | O_NONBLOCK)) == -1) { (void)fprintf(stderr, "open() error : %s\n", strerror(errno)); exit(1); } if ((writefd = open(FIFOT_NAME, O_WRONLY)) == -1) { (void)fprintf(stderr, "open() error : %s\n", strerror(errno)); exit(1); } if ((val = fcntl(readfd, F_GETFD, 0)) == -1) { (void)fprintf(stderr, "fcntl() error : %s\n", strerror(errno)); exit(1); } val &= ~O_NONBLOCK; if (fcntl(readfd, F_SETFD, val) == -1) { (void)fprintf(stderr, "fcntl() error : %s\n", strerror(errno)); exit(1); } if (argc == 1) { int ret; char buffer[13]; fd_set rset; FD_ZERO(&rset); FD_SET(readfd, &rset); ret = select(readfd + 1, &rset, NULL, NULL, NULL); if (FD_ISSET(readfd, &rset)) { if ((ret = read(readfd, &buffer, 12)) != 12) { (void)fprintf(stderr, "read() error (%d) : %s\n", ret, strerror(errno)); exit(1); } buffer[12] = '\0'; (void)fprintf(stdout, "Have received `%s'\n", buffer); } else { (void)fprintf(stderr, "select() error : %s\n", strerror(errno)); exit(1); } } else { if (write(writefd, "Hallo Ballo!", 12) != 12) { (void)fprintf(stderr, "write() error : %s\n", strerror(errno)); exit(1); } } exit(0); }