-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoll_test.c
65 lines (53 loc) · 1.41 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/epoll.h>
#include <fcntl.h>
#define FD_SIZE 100
#define MAX_EVENTS 10
int main (void)
{
int epfd = epoll_create (FD_SIZE);
struct epoll_event ev;
memset (&ev, 0x00, sizeof(ev));
ev.events = EPOLLIN; // 入力を待つ
ev.data.fd = STDIN_FILENO;
epoll_ctl (epfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev); //epollインスタンスにfdを追加し、イベントをfdに関連付ける。
// int val;
// if ((val = fcntl(STDIN_FILENO, F_GETFL, 0)) < 0) {
// perror ("fcntl F_GETFL");
// exit (EXIT_FAILURE);
// }
//
// val |= O_NONBLOCK; /* turn on flags */
//
// if (fcntl(STDIN_FILENO, F_SETFL, val) < 0) {
// perror ("fcntl F_SETFL");
// exit (EXIT_FAILURE);
// }
int i = 0;
int nfd = 0;
char buf[128];
struct epoll_event events [FD_SIZE];
while (1) {
// memset (buf, 0x00, sizeof(buf));
// read (STDIN_FILENO, buf, sizeof(buf));
puts ("epoll_wait");
memset (events, 0x00, sizeof(events));
nfd = epoll_wait (epfd, events, MAX_EVENTS, 5000); // epoll_waitは準備ができているファイルディスクリプタの数を返す
if (nfd == 0) {
puts ("timeout");
continue;
}
for (i = 0; i < nfd; i++) {
if (events[i].data.fd == STDIN_FILENO) {
memset (buf, 0x00, sizeof(buf));
read (STDIN_FILENO, buf, sizeof(buf));
printf ("[%s]", buf);
}
}
}
exit (EXIT_SUCCESS);
}