File: epoll.c

package info (click to toggle)
lsof 4.99.4%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,924 kB
  • sloc: ansic: 50,680; sh: 8,351; makefile: 1,194; perl: 940; awk: 214
file content (49 lines) | stat: -rw-r--r-- 1,056 bytes parent folder | download
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
#include <stdio.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char **argv) {
    int epfd = epoll_create(1);
    if (epfd < 0) {
        perror("epoll_create");
        return 1;
    }

    int pipefd[2];
    if (pipe(pipefd) < 0) {
        perror("pipe");
        return 1;
    }

    int evfd[2];
    if ((evfd[0] = dup(pipefd[0])) < 0) {
        perror("dup(pipefd[0])");
        return 1;
    }
    if ((evfd[1] = dup(pipefd[1])) < 0) {
        perror("dup(pipefd[1])");
        return 1;
    }

    struct epoll_event ev;
    ev.events = EPOLLOUT;
    ev.data.fd = evfd[1];
    if (epoll_ctl(epfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0) {
        perror("epoll_ctl<evfd[1]>");
        return 1;
    }

    ev.events = EPOLLIN;
    ev.data.fd = evfd[0];
    if (epoll_ctl(epfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0) {
        perror("epoll_ctl<evfd[0]>");
        return 1;
    }

    printf("%d %d %d %d\n", getpid(), epfd, evfd[0], evfd[1]);
    fflush(stdout);
    pause();
    return 0;
}