考虑以下代码段:
#import <pthread.h>
#import <stdio.h>
#import <sys/epoll.h>
#import <sys/eventfd.h>
#import <unistd.h>
int epollfd;
int evntfd;
void *function(void *arg) {
struct epoll_event events;
while(1) {
int c = epoll_wait(epollfd, &events, 1, -1);
if(c != -1) {
printf("%d\n", c);
break;
}
}
return NULL;
}
int main() {
evntfd = eventfd(0, 0);
epollfd = epoll_create(0);
struct epoll_event evnt = { 0 };
evnt.data.fd = evntfd;
evnt.events = EPOLLIN | EPOLLET;
epoll_ctl(epollfd, EPOLL_CTL_ADD, evntfd, &evnt);
pthread_t thread;
pthread_create(&thread, NULL, &function, NULL);
sleep(1);
unsigned long int u = 7;
write(evntfd, &u, sizeof(unsigned long int));
sleep(1);
return 0;
}
不应write()
使epoll_wait
返回不同于-1
的值吗?当我编译上面的代码并运行它时,没有打印任何内容......
答案 0 :(得分:2)
我在您的代码上运行了strace
,并发现了一个惊喜结果:
epoll_create(0) = -1 EINVAL (Invalid argument)
epoll文件描述符的大小需要大于零。这使其他一切都失败了。