-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_test.c
82 lines (69 loc) · 1.64 KB
/
event_test.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <error.h>
#include <stdio.h>
#include <pthread.h>
#include <stdint.h>
#include <sys/epoll.h>
#define handle_error(msg) \
do {perror(msg); exit(1);} while (0)
int main(int argc, char **argv)
{
int evfd, epfd, j, ret;
uint64_t u;
ssize_t s;
pid_t pid;
if (argc < 2) {
fprintf(stderr, "Usage: %s <num>...\n", argv[0]);
exit(1);
}
evfd = eventfd(0, EFD_NONBLOCK);
if (evfd == -1)
handle_error("eventfd");
epfd = epoll_create(10);
if (epfd == -1)
handle_error("epoll");
struct epoll_event ev;
ev.event = EPOLLIN;
ev.data.fd = evfd;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, evfd, &ev);
if (ret < 0)
handle_error("epoll_ctl");
pid = fork();
if (pid == 0) {
sleep(3);
for (j = 1; j < argc; j++) {
printf("Child writing %s to efd\n", argv[j]);
u = strtoull(argv[j], NULL, 0);
s = write(evfd, &u, sizeof (uint64_t));
if (s != sizeof (uint64_t))
handle_error("write");
sleep(1);
}
printf("Child completed write loop\n");
exit(0);
} else if (pid == -1){
handle_error("fork");
}
struct epoll_event events[10];
int fd, i;
printf("Parent about to read\n");
for (j = 1; j < argc; j++) {
ret = epoll_wait(epfd, events, 10, -1);
printf("get %d events\n", ret);
for (i = 0; i < ret; i ++) {
fd = events[i].data.fd;
if ((events[i].events & EPOLLIN) && (fd == evfd)) {
s = read(evfd, &u, sizeof (uint64_t));
if (s != sizeof (uint64_t))
handle_error("read");
printf("Parent read %llu (0x%llx) from evfd\n",
(unsigned long long)u,
(unsigned long long)u);
}
}
}
ret = epoll_ctl(epfd, EPOLL_CTL_DELL, evfd, NULL);
if (ret < 0)
handle_error("epoll_ctl");
wait();
exit(0);
}