请考虑以下程序:
#define _GNU_SOURCE
#include <sys/epoll.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
int verify(int result, const char *msg) {
if( result>=0 )
return result;
perror(msg);
abort();
return -1;
}
void writepipe( int fd, int num_bytes, const char *msg ) {
unsigned char buffer[num_bytes];
ssize_t num_written = verify( write(fd, buffer, num_bytes), msg );
assert( num_written==num_bytes );
}
void readpipe( int fd, int num_bytes, const char *msg ) {
unsigned char buffer[num_bytes];
ssize_t num_read = verify( read(fd, buffer, num_bytes), msg );
assert( num_read==num_bytes );
}
int main() {
int pipefds[2];
verify( pipe2(pipefds, O_NONBLOCK), "pipe creation failed" );
int epollfd = verify(epoll_create1(0), "epoll creation failed");
struct epoll_event evt;
evt.events = EPOLLIN|EPOLLET;
evt.data.u64 = 17;
verify( epoll_ctl( epollfd, EPOLL_CTL_ADD, pipefds[0], &evt ), "epoll_add failed" );
int num_events = verify( epoll_wait(epollfd, &evt, 1, 0), "epoll_wait failed" );
assert(num_events == 0);
writepipe( pipefds[1], 12, "initial filling of pipe" );
num_events = verify( epoll_wait(epollfd, &evt, 1, 0), "epoll_wait failed" );
assert(num_events == 1);
assert(evt.data.u64 == 17);
num_events = verify( epoll_wait(epollfd, &evt, 1, 0), "epoll_wait failed" );
assert(num_events == 0);
readpipe( pipefds[0], 12, "clean the data" );
num_events = verify( epoll_wait(epollfd, &evt, 1, 0), "epoll_wait failed" );
assert(num_events == 0);
writepipe( pipefds[1], 3, "write no trigger" );
num_events = verify( epoll_wait(epollfd, &evt, 1, 0), "epoll_wait on unarmed fd" );
assert(num_events == 0);
return 0;
}
最后一个断言失败。
因为我们从来没有EPOLLET
从 epoll 中读取 an,所以我期待最后一个epoll_wait
返回 0。相反,我得到了 1。
这是为什么?
来自 Ubuntu 16.10 的内核 4.13.0-39-generic。