-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoll2.cpp
30 lines (26 loc) · 1 KB
/
epoll2.cpp
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
#include <stdio.h>
#include <unistd.h>
#include <sys/epoll.h>
int main(void)
{
int epfd,nfds;
struct epoll_event ev,events[5]; //ev用于注册事件,数组用于返回要处理的事件
epfd = epoll_create(1); //只需要监听一个描述符——标准输入
ev.data.fd = STDIN_FILENO;
ev.events = EPOLLIN|EPOLLET; //监听读状态同时设置ET模式
epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev); //注册epoll事件
for(;;)
{
nfds = epoll_wait(epfd, events, 5, -1);
for(int i = 0; i < nfds; i++)
{
if(events[i].data.fd==STDIN_FILENO)
{
printf("welcome to epoll's word!\n");
ev.data.fd = STDIN_FILENO;
ev.events = EPOLLIN|EPOLLET; //设置ET模式
epoll_ctl(epfd, EPOLL_CTL_MOD, STDIN_FILENO, &ev); //重置epoll事件(ADD无效)
}
}
}
}