Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.development.system > #783

why epoll_wait delivers events more than once in ET mode on stdin

Newsgroups comp.os.linux.development.system
Date 2018-06-14 23:10 -0700
Message-ID <ffca76e7-36df-4ebd-8947-a4acd314de94@googlegroups.com> (permalink)
Subject why epoll_wait delivers events more than once in ET mode on stdin
From chenstrace <k54765254@gmail.com>

Show all headers | View raw


As my expection, the following test code should only return from epoll_wait once,  just like man page of epoll said:

"edge-triggered mode delivers events only when changes occur on the monitored file descriptor"


But in fact,  the epoll_wait returned more than once, why?



#include <stdio.h>
#include <sys/epoll.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#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 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);
        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);
}

Back to comp.os.linux.development.system | Previous | NextNext in thread | Find similar


Thread

why epoll_wait delivers events more than once in ET mode on stdin chenstrace <k54765254@gmail.com> - 2018-06-14 23:10 -0700
  Re: why epoll_wait delivers events more than once in ET mode on stdin Rainer Weikusat <rweikusat@talktalk.net> - 2018-06-15 13:15 +0100

csiph-web