Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


eventfd(2) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |VERSIONS |STANDARDS |HISTORY |NOTES |EXAMPLES |SEE ALSO |COLOPHON

eventfd(2)                 System Calls Manualeventfd(2)

NAME        top

       eventfd - create a file descriptor for event notification

LIBRARY        top

       Standard C library (libc,-lc)

SYNOPSIS        top

#include <sys/eventfd.h>int eventfd(unsigned intinitval, intflags);

DESCRIPTION        top

eventfd() creates an "eventfd object" that can be used as an event       wait/notify mechanism by user-space applications, and by the       kernel to notify user-space applications of events.  The object       contains an unsigned 64-bit integer (uint64_t) counter that is       maintained by the kernel.  This counter is initialized with the       value specified in the argumentinitval.       As its return value,eventfd() returns a new file descriptor that       can be used to refer to the eventfd object.       The following values may be bitwise ORed inflags to change the       behavior ofeventfd():EFD_CLOEXEC(since Linux 2.6.27)              Set the close-on-exec (FD_CLOEXEC) flag on the new file              descriptor.  See the description of theO_CLOEXECflag inopen(2) for reasons why this may be useful.EFD_NONBLOCK(since Linux 2.6.27)              Set theO_NONBLOCKfile status flag on the open file              description (seeopen(2)) referred to by the new file              descriptor.  Using this flag saves extra calls tofcntl(2)              to achieve the same result.EFD_SEMAPHORE(since Linux 2.6.30)              Provide semaphore-like semantics for reads from the new              file descriptor.  See below.       Up to Linux 2.6.26, theflags argument is unused, and must be       specified as zero.       The following operations can be performed on the file descriptor       returned byeventfd():read(2)              Each successfulread(2) returns an 8-byte integer.  Aread(2) fails with the errorEINVALif the size of the              supplied buffer is less than 8 bytes.              The value returned byread(2) is in host byte order—that              is, the native byte order for integers on the host machine.              The semantics ofread(2) depend on whether the eventfd              counter currently has a nonzero value and whether theEFD_SEMAPHOREflag was specified when creating the eventfd              file descriptor:              •  IfEFD_SEMAPHOREwas not specified and the eventfd                 counter has a nonzero value, then aread(2) returns 8                 bytes containing that value, and the counter's value is                 reset to zero.              •  IfEFD_SEMAPHOREwas specified and the eventfd counter                 has a nonzero value, then aread(2) returns 8 bytes                 containing the value 1, and the counter's value is                 decremented by 1.              •  If the eventfd counter is zero at the time of the call                 toread(2), then the call either blocks until the                 counter becomes nonzero (at which time, theread(2)                 proceeds as described above) or fails with the errorEAGAINif the file descriptor has been made nonblocking.write(2)              Awrite(2) call adds the 8-byte integer value supplied in              its buffer to the counter.  The maximum value that may be              stored in the counter is the largest unsigned 64-bit value              minus 1 (i.e., 0xfffffffffffffffe).  If the addition would              cause the counter's value to exceed the maximum, then thewrite(2) either blocks until aread(2) is performed on the              file descriptor, or fails with the errorEAGAINif the file              descriptor has been made nonblocking.              Awrite(2) fails with the errorEINVALif the size of the              supplied buffer is less than 8 bytes, or if an attempt is              made to write the value 0xffffffffffffffff.poll(2)select(2)       (and similar)              The returned file descriptor supportspoll(2) (and              analogouslyepoll(7)) andselect(2), as follows:              •  The file descriptor is readable (theselect(2)readfds                 argument; thepoll(2)POLLINflag) if the counter has a                 value greater than 0.              •  The file descriptor is writable (theselect(2)writefds                 argument; thepoll(2)POLLOUTflag) if it is possible to                 write a value of at least "1" without blocking.              •  If an overflow of the counter value was detected, thenselect(2) indicates the file descriptor as being both                 readable and writable, andpoll(2) returns aPOLLERR                 event.  As noted above,write(2) can never overflow the                 counter.  However an overflow can occur if 2^64 eventfd                 "signal posts" were performed by the KAIO subsystem                 (theoretically possible, but practically unlikely).  If                 an overflow has occurred, thenread(2) will return that                 maximumuint64_t value (i.e., 0xffffffffffffffff).              The eventfd file descriptor also supports the other file-              descriptor multiplexing APIs:pselect(2) andppoll(2).close(2)              When the file descriptor is no longer required it should be              closed.  When all file descriptors associated with the same              eventfd object have been closed, the resources for object              are freed by the kernel.       A copy of the file descriptor created byeventfd() is inherited by       the child produced byfork(2).  The duplicate file descriptor is       associated with the same eventfd object.  File descriptors created       byeventfd() are preserved acrossexecve(2), unless the close-on-       exec flag has been set.

RETURN VALUE        top

       On success,eventfd() returns a new eventfd file descriptor.  On       error, -1 is returned anderrno is set to indicate the error.

ERRORS        top

EINVALAn unsupported value was specified inflags.EMFILEThe per-process limit on the number of open file              descriptors has been reached.ENFILEThe system-wide limit on the total number of open files has              been reached.ENODEVCould not mount (internal) anonymous inode device.ENOMEMThere was insufficient memory to create a new eventfd file              descriptor.

ATTRIBUTES        top

       For an explanation of the terms used in this section, seeattributes(7).       ┌──────────────────────────────────────┬───────────────┬─────────┐       │InterfaceAttributeValue│       ├──────────────────────────────────────┼───────────────┼─────────┤       │eventfd()                            │ Thread safety │ MT-Safe │       └──────────────────────────────────────┴───────────────┴─────────┘

VERSIONS        top

C library/kernel differences       There are two underlying Linux system calls:eventfd() and the       more recenteventfd2().  The former system call does not implement       aflags argument.  The latter system call implements theflags       values described above.  The glibc wrapper function will useeventfd2() where it is available.Additional glibc features       The GNU C library defines an additional type, and two functions       that attempt to abstract some of the details of reading and       writing on an eventfd file descriptor:           typedef uint64_t eventfd_t;           int eventfd_read(int fd, eventfd_t *value);           int eventfd_write(int fd, eventfd_t value);       The functions perform the read and write operations on an eventfd       file descriptor, returning 0 if the correct number of bytes was       transferred, or -1 otherwise.

STANDARDS        top

       Linux, GNU.

HISTORY        top

eventfd()              Linux 2.6.22, glibc 2.8.eventfd2()              Linux 2.6.27 (see VERSIONS).  Since glibc 2.9, theeventfd() wrapper will employ theeventfd2() system call,              if it is supported by the kernel.

NOTES        top

       Applications can use an eventfd file descriptor instead of a pipe       (seepipe(2)) in all cases where a pipe is used simply to signal       events.  The kernel overhead of an eventfd file descriptor is much       lower than that of a pipe, and only one file descriptor is       required (versus the two required for a pipe).       When used in the kernel, an eventfd file descriptor can provide a       bridge from kernel to user space, allowing, for example,       functionalities like KAIO (kernel AIO) to signal to a file       descriptor that some operation is complete.       A key point about an eventfd file descriptor is that it can be       monitored just like any other file descriptor usingselect(2),poll(2), orepoll(7).  This means that an application can       simultaneously monitor the readiness of "traditional" files and       the readiness of other kernel mechanisms that support the eventfd       interface.  (Without theeventfd() interface, these mechanisms       could not be multiplexed viaselect(2),poll(2), orepoll(7).)       The current value of an eventfd counter can be viewed via the       entry for the corresponding file descriptor in the process's/proc/pid/fdinfo directory.  Seeproc(5) for further details.

EXAMPLES        top

       The following program creates an eventfd file descriptor and then       forks to create a child process.  While the parent briefly sleeps,       the child writes each of the integers supplied in the program's       command-line arguments to the eventfd file descriptor.  When the       parent has finished sleeping, it reads from the eventfd file       descriptor.       The following shell session shows a sample run of the program:           $./a.out 1 2 4 7 14           Child writing 1 to efd           Child writing 2 to efd           Child writing 4 to efd           Child writing 7 to efd           Child writing 14 to efd           Child completed write loop           Parent about to read           Parent read 28 (0x1c) from efdProgram source       #include <err.h>       #include <inttypes.h>       #include <stdio.h>       #include <stdlib.h>       #include <sys/eventfd.h>       #include <sys/types.h>       #include <unistd.h>       int       main(int argc, char *argv[])       {           int       efd;           uint64_t  u;           ssize_t   s;           if (argc < 2) {               fprintf(stderr, "Usage: %s <num>...\n", argv[0]);               exit(EXIT_FAILURE);           }           efd = eventfd(0, 0);           if (efd == -1)               err(EXIT_FAILURE, "eventfd");           switch (fork()) {           case 0:               for (size_t j = 1; j < argc; j++) {                   printf("Child writing %s to efd\n", argv[j]);                   u = strtoull(argv[j], NULL, 0);                           /* strtoull() allows various bases */                   s = write(efd, &u, sizeof(uint64_t));                   if (s != sizeof(uint64_t))                       err(EXIT_FAILURE, "write");               }               printf("Child completed write loop\n");               exit(EXIT_SUCCESS);           default:               sleep(2);               printf("Parent about to read\n");               s = read(efd, &u, sizeof(uint64_t));               if (s != sizeof(uint64_t))                   err(EXIT_FAILURE, "read");               printf("Parent read %"PRIu64" (%#"PRIx64") from efd\n", u, u);               exit(EXIT_SUCCESS);           case -1:               err(EXIT_FAILURE, "fork");           }       }

SEE ALSO        top

futex(2),pipe(2),poll(2),read(2),select(2),signalfd(2),timerfd_create(2),write(2),epoll(7),sem_overview(7)

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-05-17eventfd(2)

Pages that refer to this page:io_uring_register(2)signalfd(2)syscalls(2)timerfd_create(2)io_uring_register_eventfd(3)io_uring_register_eventfd_async(3)io_uring_unregister_eventfd(3)proc_pid_fd(5)proc_pid_fdinfo(5)systemd.exec(5)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp