-
Notifications
You must be signed in to change notification settings - Fork 8
/
epoll_test.c
45 lines (33 loc) · 909 Bytes
/
epoll_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
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <unistd.h>
int main(int argc, char **argv[])
{
int ret;
int stdin_fd = 0;
int ep_fd;
ep_fd = epoll_create1(EPOLL_CLOEXEC);
if (ep_fd < 0)
return -1;
struct epoll_event ep_events;
memset(&ep_events, 0, sizeof(struct epoll_event));
ep_events.events = EPOLLIN;
ep_events.data.fd = stdin_fd;
if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, stdin_fd, &ep_events) < 0)
return -1;
for (;;) {
int fds;
struct epoll_event evt;
fds = epoll_wait(ep_fd, &evt, 1, -1);
printf("fds %d\n", fds);
if (evt.data.fd == stdin_fd) {
printf("read from stdin\n");
char buf[100];
memset(buf, 0, sizeof(buf));
read(stdin_fd, buf, sizeof(buf));
printf("input %s\n", buf);
}
}
return 0;
}