public
Created

epoll_ctl with EPOLLET on an already readable file

  • Download Gist
epoll-example-pipe.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// compile with: gcc epoll-example-pipe.c
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
 
#define MAXEVENTS 64
 
int
main (int argc, char *argv[])
{
struct epoll_event event;
struct epoll_event *events;
int pfds[2];
char buf[30];
pipe(pfds);
if (!fork()) {
printf(" CHILD: sleeping\n");
sleep(3);
printf(" CHILD: writing to the pipe\n");
write(pfds[1], "test", 5);
printf(" CHILD: exiting\n");
exit(0);
} else {
int efd, n, s;
 
// create epoll instance.
efd = epoll_create1 (0);
// allocate events array
events = calloc (MAXEVENTS, sizeof event);
// register read end of pipe using edge-triggered mode with ONESHOT.
event.data.fd = pfds[0];
event.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
s = epoll_ctl (efd, EPOLL_CTL_ADD, pfds[0], &event);
 
// wait on read-end becoming readable.
printf("PARENT: waiting on pipe\n");
n = epoll_wait (efd, events, MAXEVENTS, -1);
printf("PARENT: epoll_wait success; n=%d.\n", n);
 
// don't read anything, just re-arm the FD and wait again.
epoll_ctl (efd, EPOLL_CTL_MOD, pfds[0], &event);
printf("PARENT: waiting on pipe\n");
n = epoll_wait (efd, events, MAXEVENTS, -1);
printf("PARENT: epoll_wait success; n=%d.\n", n);
wait(NULL);
}
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.