-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoll-dup-example.c
50 lines (41 loc) · 1.04 KB
/
epoll-dup-example.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <unistd.h>
int main() {
int pfd[2];
pipe(pfd);
int rfd = pfd[0];
int wfd = pfd[1];
/* Write make the pipe readable. */
char buf[1] = {0};
write(wfd, buf, sizeof(buf));
/* */
int epfd = epoll_create(1);
struct epoll_event ev = {.events = EPOLLIN, .data.fd = rfd};
epoll_ctl(epfd, EPOLL_CTL_ADD, rfd, &ev);
int rfd2 = dup(rfd);
printf("close(fd=%d)\n", rfd);
close(rfd);
int r = epoll_wait(epfd, &ev, 1, 1);
if (r == 0) {
printf("epoll_wait() finished with no events... okay...\n");
} else {
printf("epoll_wait() got Event! on fd %d! wait...\n", ev.data.fd);
}
r = epoll_ctl(epfd, EPOLL_CTL_DEL, rfd, NULL);
if (r != 0) {
perror("epoll_ctl(EPOLL_CTL_DEL, rfd)");
}
r = epoll_ctl(epfd, EPOLL_CTL_DEL, rfd2, NULL);
if (r != 0) {
perror("epoll_ctl(EPOLL_CTL_DEL, rfd2)");
}
// Bonus points!!!!!
rfd = dup(rfd2);
r = epoll_ctl(epfd, EPOLL_CTL_DEL, rfd, NULL);
if (r == 0) {
printf("epoll_ctl(EPOLL_CTL_DEL, rfd)... okay? how come?\n");
}
return 0;
}