2

Considering the following code snippet:

#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;
}

shouldn't write() make epoll_wait return a value different than -1? When I compile the code above and run it, nothing is printed...

4

1 回答 1

3

我运行strace了您的代码,发现了一个令人惊讶的结果:

epoll_create(0)                         = -1 EINVAL (Invalid argument)

您的 epoll 文件描述符的大小需要大于零。这使其他一切都失败了。

于 2013-06-01T04:03:13.430 回答