123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #include <fcntl.h>
- #include <stdio.h>
- #include <sys/epoll.h>
- #include <sys/wait.h>
- #include <unistd.h>
- int reader(int fd) {
-
- int epollfd = epoll_create1(EPOLL_CLOEXEC);
- if (epollfd < 0) {
- perror("epoll_create1");
- return 1;
- }
-
- struct epoll_event ev;
- ev.events = EPOLLIN;
- ev.data.fd = fd;
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
- perror("epoll_ctl");
- return 1;
- }
-
- struct epoll_event events[8];
- for (int i = 0; i < 1024; i++) {
-
- int nfds = epoll_wait(epollfd, events, sizeof(events)/sizeof(struct epoll_event), -1);
- if (nfds < 0) {
- perror("epoll_wait");
- return 1;
- }
-
- for (int n = 0; n < nfds; n++) {
-
- if (events[n].data.fd == fd) {
-
- int writer_i;
- int count = read(fd, &writer_i, sizeof(writer_i));
- if (count < 0) {
- perror("read");
- return 1;
- } else if (count < sizeof(writer_i)) {
- fprintf(stderr, "read %d instead of %d\n", count, sizeof(writer_i));
- return 1;
- }
-
- if (i != writer_i) {
- fprintf(stderr, "received event count %d instead of %d\n", writer_i, i);
- return 1;
- }
- printf("%d == %d\n", i, writer_i);
- } else {
-
- fprintf(stderr, "unknown fd %d\n", events[n].data.fd);
- return 1;
- }
- }
- }
- return 0;
- }
- int writer(int fd) {
-
- int epollfd = epoll_create1(EPOLL_CLOEXEC);
- if (epollfd < 0) {
- perror("epoll_create1");
- return 1;
- }
-
- struct epoll_event ev;
- ev.events = EPOLLOUT;
- ev.data.fd = fd;
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
- perror("epoll_ctl");
- return 1;
- }
-
- struct epoll_event events[8];
- for (int i = 0; i < 1024; i++) {
-
- int nfds = epoll_wait(epollfd, events, sizeof(events)/sizeof(struct epoll_event), -1);
- if (nfds < 0) {
- perror("epoll_wait");
- return 1;
- }
-
- for (int n = 0; n < nfds; n++) {
-
- if (events[n].data.fd == fd) {
-
- int count = write(fd, &i, sizeof(i));
- if (count < 0) {
- perror("write");
- return 1;
- } else if (count < sizeof(i)) {
- fprintf(stderr, "wrote %d instead of %d\n", count, sizeof(i));
- return 1;
- }
- } else {
-
- fprintf(stderr, "unknown fd %d\n", events[n].data.fd);
- return 1;
- }
- }
- }
- return 0;
- }
- int main(int argc, char **argv) {
-
- int pipefd[2];
- if (pipe2(pipefd, O_CLOEXEC | O_NONBLOCK) < 0) {
- perror("pipe2");
- return 1;
- }
- pid_t pid = fork();
- if (pid < 0) {
- perror("fork");
- return 1;
- } else if (pid == 0) {
-
- close(pipefd[1]);
- return reader(pipefd[0]);
- } else {
-
- close(pipefd[0]);
- int ret = writer(pipefd[1]);
-
- int status = 0;
- if (waitpid(pid, &status, 0) != pid) {
- perror("waitpid");
- return 1;
- }
-
- if (ret != 0) {
- return ret;
- }
-
- if (WIFEXITED(status)) {
-
- return WEXITSTATUS(status);
- } else {
-
- return 1;
- }
- }
- }
|