/* * epoll_pipe_pingpong by Davide Libenzi (Another epoll and pipes test) * Copyright (C) 2009 Davide Libenzi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Davide Libenzi * */ #include #include #include #include #include #include #include #include #include #include #include #include #define EPWAIT_TIMEO (1 * 1000) static int xepoll_create(int n) { int epfd; if ((epfd = epoll_create(n)) == -1) { perror("epoll_create"); exit(2); } return epfd; } static void xepoll_ctl(int epfd, int cmd, int fd, struct epoll_event *evt) { if (epoll_ctl(epfd, cmd, fd, evt) < 0) { perror("epoll_ctl"); exit(3); } } static void xpipe(int *fds) { if (pipe(fds)) { perror("pipe"); exit(4); } } static int run_test(void) { int n, epfd, loops; int pfds[2]; char rdb[2]; struct epoll_event evt, evts[8]; epfd = xepoll_create(1); xpipe(pfds); fcntl(pfds[0], F_SETFL, fcntl(pfds[0], F_GETFL, 0) | O_NONBLOCK); memset(&evt, 0, sizeof(evt)); evt.data.fd = pfds[0]; evt.events = EPOLLIN; xepoll_ctl(epfd, EPOLL_CTL_ADD, pfds[0], &evt); evt.data.fd = pfds[1]; evt.events = EPOLLOUT; xepoll_ctl(epfd, EPOLL_CTL_ADD, pfds[1], &evt); for (loops = 100; loops > 0; loops--) { n = epoll_wait(epfd, evts, 8, EPWAIT_TIMEO); if (n <= 0) { fprintf(stderr, "Aiee!! Why no events?!\n"); break; } for (; n > 0; n--) { struct epoll_event *pevt = &evts[n - 1]; if (pevt->data.fd == pfds[1] && pevt->events & EPOLLOUT) { fprintf(stdout, "Write side, strong side!\n"); write(pfds[1], "w", 1); } else if (pevt->data.fd == pfds[0] && pevt->events & EPOLLIN) { fprintf(stdout, "Read side, strong side!\n"); while (read(pfds[0], rdb, sizeof(rdb)) > 0); } } } return 0; } int main(int ac, char **av) { return run_test(); }