Question

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...

Was it helpful?

Solution

I ran strace on your code, and found a surprise result:

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

The size of your epoll file descriptor needs to be greater than zero. This is making everything else fail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top