epoll监听文件_怎么理解把标准输入以ET模式加入epoll,监听EPOLLOUT事件时,epoll_wait多次返回?...

确实是后面的printf导致的,去掉后面的printf,增加计数器的判断, 可确认epoll_wait只返回了一次。但是为什么printf会影响到标准输入stdin?还是不理解。

修改后符合预期代码如下,epoll_wait只返回一次。

#include

#include

#include

#include

#include

#include

#include

#include

#define MAX_EVENTS 512

static char* get_epoll_events_str(uint32_t event);

int main(int argc, char *argv[]) {

int i,nfds;

struct epoll_event event;

struct epoll_event events[MAX_EVENTS];

int fd = STDIN_FILENO;

int count = 0;

int epfd = epoll_create1(0);

if (epfd == -1) {

perror("epoll_create1");

exit(EXIT_FAILURE);

}

event.events = EPOLLOUT | EPOLLET;

event.data.u64 = 123;

int ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);

if (ret == -1) {

perror("epoll_ctl");

exit(EXIT_FAILURE);

}

while (1) {

nfds = epoll_wait(epfd, events, MAX_EVENTS, -1);

count++;

if(count >=2) {printf("should not come here,exit\n");exit(0);}

if (nfds == -1) {

perror("epoll_wait");

exit(EXIT_FAILURE);

}

// printf("epoll_wait return %d\n", nfds);

for (i = 0; i < nfds; i++) {

char* str = get_epoll_events_str(events[i].events);

// printf("%dth fd's data.u64=%lu,event is %s\n",i,events[i].data.u64,str);

free(str);

}

// sleep(1);

}

return 0;

}

static char* get_epoll_events_str(uint32_t event) {

char str[4096] = {0};

if(event == 0)

{

return NULL;

}

if (event & EPOLLIN) strcat(str, "EPOLLIN,");

if (event & EPOLLPRI) strcat(str, "EPOLLPRI,");

if (event & EPOLLOUT) strcat(str, "EPOLLOUT,");

if (event & EPOLLRDNORM) strcat(str, "EPOLLRDNORM,");

if (event & EPOLLRDBAND) strcat(str, "EPOLLRDBAND,");

if (event & EPOLLWRNORM) strcat(str, "EPOLLWRNORM,");

if (event & EPOLLWRBAND) strcat(str, "EPOLLWRBAND,");

if (event & EPOLLMSG) strcat(str, "EPOLLMSG,");

if (event & EPOLLERR) strcat(str, "EPOLLERR,");

if (event & EPOLLHUP) strcat(str, "EPOLLHUP,");

if (event & EPOLLRDHUP) strcat(str, "EPOLLRDHUP,");

if (event & EPOLLWAKEUP) strcat(str, "EPOLLWAKEUP,");

if (event & EPOLLONESHOT) strcat(str, "EPOLLONESHOT,");

if (event & EPOLLET) strcat(str, "EPOLLET,");

if(strlen(str) == 0)

{

return NULL;

}

return strdup(str);

}

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页