better handling of whole files
[tridge/junkcode.git] / epoll_fork.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <sys/epoll.h>
6 #include <sys/wait.h>
7 #include <errno.h>
8
9 static void run_child(int epoll_fd)
10 {
11         struct epoll_event event;
12         int fd[2];
13         int ret;
14
15         pipe(fd);
16
17         memset(&event, 0, sizeof(event));
18
19         event.events = EPOLLIN|EPOLLERR;
20         event.data.u32 = getpid();
21
22         if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd[0], &event) != 0) {
23                 perror("epoll_ctl");
24                 abort();
25         }
26
27         while (1) {
28                 char c = 0;
29                 write(fd[1], &c, 1);
30                 ret = epoll_wait(epoll_fd, &event, 1, 10);
31                 if (ret <= 0) {
32                         continue;
33                 }
34                 if (event.data.u32 != getpid()) {
35                         printf("Wrong pid! should be %u but got %u\n", 
36                                getpid(), event.data.u32);
37                         exit(0);
38                 }
39         }
40         exit(0);
41 }
42
43
44 int main(void) 
45 {
46         int epoll_fd;
47         pid_t child1, child2;
48
49         epoll_fd = epoll_create(64);
50
51         child1 = fork();
52         if (child1 == 0) {
53                 run_child(epoll_fd);
54         }
55         child2 = fork();
56         if (child2 == 0) {
57                 run_child(epoll_fd);
58         }
59
60         waitpid(0, NULL, 0);
61         kill(child1, SIGTERM);
62         kill(child2, SIGTERM);
63
64         return 0;
65 }