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