eventfd2 - Linux


eventfd2 is a Linux system call that creates an eventfd object, a type of file descriptor that can be used to efficiently notify userspace processes of events. It provides a more flexible and efficient way to signal events compared to traditional methods like pipes or signals.


int eventfd2(unsigned int initval, int flags);


  • initval: Initial value for the eventfd object.
  • flags: Flags to control the behavior of the eventfd object. They can be any combination of the following:
    • EFD_CLOEXEC: Close the eventfd on exec.
    • EFD_NONBLOCK: Make the eventfd non-blocking.


Simple event signaling:

#include <sys/eventfd.h>

int main() {
    int efd = eventfd(0, 0); // Create eventfd with initial value 0
    uint64_t val;
    write(efd, &val, sizeof(val)); // Signal an event
    read(efd, &val, sizeof(val)); // Wait for and read the event
    return 0;

Non-blocking event signaling:

#include <sys/eventfd.h>
#include <fcntl.h>

int main() {
    int efd = eventfd(0, EFD_NONBLOCK); // Create non-blocking eventfd
    uint64_t val = 1;
    int ret = write(efd, &val, sizeof(val)); // Signal an event
    if (ret == -1 && errno == EAGAIN) {
        // Non-blocking write failed, handle accordingly
    return 0;

Common Issues

  • Non-blocking write error: If the eventfd is non-blocking and the buffer is full, the write() operation will fail with EAGAIN.
  • Unexpected event count: Ensure that the eventfd is only signaled when intended, as multiple signals can accumulate.


eventfd2 can be combined with other commands or tools for advanced tasks, such as:

  • epoll: Monitor eventfds for events using epoll.
  • inotify: Use eventfds to efficiently signal file system events.

Related Commands

  • eventfd: Legacy eventfd system call with limited features.
  • epoll_create1: Create an epoll instance that can monitor eventfds.