altio/multithread_epoll_wait.c

This is altio/multithread_epoll_wait.c, an example to accompany the book, The Linux Programming Interface.

This file is not printed in the book; it is a supplementary file for Chapter 63.

The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3.

In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.

 

Download altio/multithread_epoll_wait.c

  Cover of The Linux Programming Interface

Function list (Bold in this list means a function is not static)

/* multithread_epoll_wait.c

   If multiple threads are waiting in epoll_wait() on the same FD, then,
   with EPOLLET (edge-triggered notification), only one of the threads
   is woken up when I/O activity occurs. By contrast, with level-triggered
   notification, all threads are woken up.

   To test this:

        $ ./multithread_epoll_wait x            # With EPOLLET
        Thread 0 about to epoll_wait()
        Thread 1 about to epoll_wait()
        Thread 2 about to epoll_wait()
        Thread 3 about to epoll_wait()
        Thread 4 about to epoll_wait()

        main() about to write a byte to pipe

        Thread 4 completed epoll_wait(); ready = 1
        main() about to terminate

        $ ./multithread_epoll_wait              # Without EPOLLET
        Thread 1 about to epoll_wait()
        Thread 4 about to epoll_wait()
        Thread 0 about to epoll_wait()
        Thread 3 about to epoll_wait()
        Thread 2 about to epoll_wait()

        main() about to write a byte to pipe

        Thread 2 completed epoll_wait(); ready = 1
        Thread 3 completed epoll_wait(); ready = 1
        Thread 0 completed epoll_wait(); ready = 1
        Thread 4 completed epoll_wait(); ready = 1
        Thread 1 completed epoll_wait(); ready = 1
        main() about to terminate
*/
#include <sys/epoll.h>
#include <fcntl.h>
#include <pthread.h>
#include "tlpi_hdr.h"

#define MAX_EVENTS     5      /* Max. # of events we allow to be returned
                                 from a single epoll_wait() call */

static int pipe1[2];
static int epfd;
static void *
threadFunc(void *arg)
{
    struct epoll_event evlist[MAX_EVENTS];
    long tnum = (long) arg;

    printf("Thread %ld about to epoll_wait()\n", tnum);
    int ready = epoll_wait(epfd, evlist, MAX_EVENTS, -1);
    if (ready == -1)
        errExit("epoll_wait");
    printf("Thread %ld completed epoll_wait(); ready = %d\n", tnum, ready);

    return NULL;
}
int
main(int argc, char *argv[])
{
    int epollet = (argc > 1) ? EPOLLET : 0;

    epfd = epoll_create(5);
    if (epfd == -1)
        errExit("epoll_create");

    if (pipe(pipe1) == -1)
        errExit("pipe1");

    struct epoll_event ev;
    ev.events = EPOLLIN | epollet;      /* Only interested in input events */
    ev.data.fd = pipe1[0];
    if (epoll_ctl(epfd, EPOLL_CTL_ADD, pipe1[0], &ev) == -1)
        errExit("epoll_ctl");

    for (long j = 0; j < 5; j++) {
        pthread_t t1;
        int s = pthread_create(&t1, NULL, threadFunc, (void *) j);
        if (s != 0)
            errExitEN(s, "pthread_create");
    }

    sleep(2);

    printf("\nmain() about to write a byte to pipe\n\n");
    write(pipe1[1], "x", 1);
    sleep(2);
    printf("main() about to terminate\n");

    exit(EXIT_SUCCESS);

}

 

Download altio/multithread_epoll_wait.c

Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.

Valid XHTML 1.1