1 .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" starting from a version by Davide Libenzi <davidel@xmailserver.org>
4 .\" SPDX-License-Identifier: GPL-2.0-or-later
6 .\" 2008-10-10, mtk: describe eventfd2(), and EFD_NONBLOCK and EFD_CLOEXEC
8 .TH EVENTFD 2 2021-03-22 Linux "Linux Programmer's Manual"
10 eventfd \- create a file descriptor for event notification
13 .RI ( libc ", " \-lc )
16 .B #include <sys/eventfd.h>
18 .BI "int eventfd(unsigned int " initval ", int " flags );
22 creates an "eventfd object" that can be used as
23 an event wait/notify mechanism by user-space applications,
24 and by the kernel to notify user-space applications of events.
25 The object contains an unsigned 64-bit integer
27 counter that is maintained by the kernel.
28 This counter is initialized with the value specified in the argument
33 returns a new file descriptor that can be used to refer to the
36 The following values may be bitwise ORed in
38 to change the behavior of
41 .BR EFD_CLOEXEC " (since Linux 2.6.27)"
44 flag on the new file descriptor.
45 See the description of the
49 for reasons why this may be useful.
51 .BR EFD_NONBLOCK " (since Linux 2.6.27)"
54 file status flag on the open file description (see
56 referred to by the new file descriptor.
57 Using this flag saves extra calls to
59 to achieve the same result.
61 .BR EFD_SEMAPHORE " (since Linux 2.6.30)"
62 Provide semaphore-like semantics for reads from the new file descriptor.
65 In Linux up to version 2.6.26, the
67 argument is unused, and must be specified as zero.
69 The following operations can be performed on the file descriptor returned by
75 returns an 8-byte integer.
80 if the size of the supplied buffer is less than 8 bytes.
84 is in host byte order\(emthat is,
85 the native byte order for integers on the host machine.
89 depend on whether the eventfd counter currently has a nonzero value
92 flag was specified when creating the eventfd file descriptor:
97 was not specified and the eventfd counter has a nonzero value, then a
99 returns 8 bytes containing that value,
100 and the counter's value is reset to zero.
104 was specified and the eventfd counter has a nonzero value, then a
106 returns 8 bytes containing the value 1,
107 and the counter's value is decremented by 1.
109 If the eventfd counter is zero at the time of the call to
111 then the call either blocks until the counter becomes nonzero
114 proceeds as described above)
115 or fails with the error
117 if the file descriptor has been made nonblocking.
123 call adds the 8-byte integer value supplied in its
124 buffer to the counter.
125 The maximum value that may be stored in the counter is the largest
126 unsigned 64-bit value minus 1 (i.e., 0xfffffffffffffffe).
127 If the addition would cause the counter's value to exceed
128 the maximum, then the
130 either blocks until a
132 is performed on the file descriptor,
133 or fails with the error
135 if the file descriptor has been made nonblocking.
141 if the size of the supplied buffer is less than 8 bytes,
142 or if an attempt is made to write the value 0xffffffffffffffff.
144 .BR poll "(2), " select "(2) (and similar)"
145 The returned file descriptor supports
154 The file descriptor is readable
162 if the counter has a value greater than 0.
164 The file descriptor is writable
172 if it is possible to write a value of at least "1" without blocking.
174 If an overflow of the counter value was detected,
177 indicates the file descriptor as being both readable and writable, and
184 can never overflow the counter.
185 However an overflow can occur if 2^64
186 eventfd "signal posts" were performed by the KAIO
187 subsystem (theoretically possible, but practically unlikely).
188 If an overflow has occurred, then
190 will return that maximum
192 value (i.e., 0xffffffffffffffff).
195 The eventfd file descriptor also supports the other file-descriptor
202 When the file descriptor is no longer required it should be closed.
203 When all file descriptors associated with the same eventfd object
204 have been closed, the resources for object are freed by the kernel.
206 A copy of the file descriptor created by
208 is inherited by the child produced by
210 The duplicate file descriptor is associated with the same
212 File descriptors created by
216 unless the close-on-exec flag has been set.
220 returns a new eventfd file descriptor.
221 On error, \-1 is returned and
223 is set to indicate the error.
227 An unsupported value was specified in
231 The per-process limit on the number of open file descriptors has been reached.
234 The system-wide limit on the total number of open files has been
238 .\" Note from Davide:
239 .\" The ENODEV error is basically never going to happen if
240 .\" the kernel boots correctly. That error happen only if during
241 .\" the kernel initialization, some error occur in the anonymous
242 .\" inode source initialization.
243 Could not mount (internal) anonymous inode device.
246 There was insufficient memory to create a new
247 eventfd file descriptor.
250 is available on Linux since kernel 2.6.22.
251 Working support is provided in glibc since version 2.8.
252 .\" eventfd() is in glibc 2.7, but reportedly does not build
255 system call (see NOTES) is available on Linux since kernel 2.6.27.
256 Since version 2.9, the glibc
258 wrapper will employ the
260 system call, if it is supported by the kernel.
262 For an explanation of the terms used in this section, see
270 Interface Attribute Value
273 T} Thread safety MT-Safe
284 Applications can use an eventfd file descriptor instead of a pipe (see
286 in all cases where a pipe is used simply to signal events.
287 The kernel overhead of an eventfd file descriptor
288 is much lower than that of a pipe,
289 and only one file descriptor is
290 required (versus the two required for a pipe).
292 When used in the kernel, an eventfd
293 file descriptor can provide a bridge from kernel to user space, allowing,
294 for example, functionalities like KAIO (kernel AIO)
295 .\" or eventually syslets/threadlets
296 to signal to a file descriptor that some operation is complete.
298 A key point about an eventfd file descriptor is that it can be
299 monitored just like any other file descriptor using
304 This means that an application can simultaneously monitor the
305 readiness of "traditional" files and the readiness of other
306 kernel mechanisms that support the eventfd interface.
309 interface, these mechanisms could not be multiplexed via
315 The current value of an eventfd counter can be viewed
316 via the entry for the corresponding file descriptor in the process's
317 .IR /proc/ pid /fdinfo
323 .SS C library/kernel differences
324 There are two underlying Linux system calls:
328 The former system call does not implement a
331 The latter system call implements the
333 values described above.
334 The glibc wrapper function will use
336 where it is available.
337 .SS Additional glibc features
338 The GNU C library defines an additional type,
339 and two functions that attempt to abstract some of the details of
340 reading and writing on an eventfd file descriptor:
344 typedef uint64_t eventfd_t;
346 int eventfd_read(int fd, eventfd_t *value);
347 int eventfd_write(int fd, eventfd_t value);
351 The functions perform the read and write operations on an
352 eventfd file descriptor,
353 returning 0 if the correct number of bytes was transferred,
356 The following program creates an eventfd file descriptor
357 and then forks to create a child process.
358 While the parent briefly sleeps,
359 the child writes each of the integers supplied in the program's
360 command-line arguments to the eventfd file descriptor.
361 When the parent has finished sleeping,
362 it reads from the eventfd file descriptor.
364 The following shell session shows a sample run of the program:
368 .RB "$" " ./a.out 1 2 4 7 14"
369 Child writing 1 to efd
370 Child writing 2 to efd
371 Child writing 4 to efd
372 Child writing 7 to efd
373 Child writing 14 to efd
374 Child completed write loop
376 Parent read 28 (0x1c) from efd
381 .\" SRC BEGIN (eventfd.c)
383 #include <inttypes.h>
386 #include <sys/eventfd.h>
389 #define handle_error(msg) \e
390 do { perror(msg); exit(EXIT_FAILURE); } while (0)
393 main(int argc, char *argv[])
400 fprintf(stderr, "Usage: %s <num>...\en", argv[0]);
406 handle_error("eventfd");
410 for (int j = 1; j < argc; j++) {
411 printf("Child writing %s to efd\en", argv[j]);
412 u = strtoull(argv[j], NULL, 0);
413 /* strtoull() allows various bases */
414 s = write(efd, &u, sizeof(uint64_t));
415 if (s != sizeof(uint64_t))
416 handle_error("write");
418 printf("Child completed write loop\en");
425 printf("Parent about to read\en");
426 s = read(efd, &u, sizeof(uint64_t));
427 if (s != sizeof(uint64_t))
428 handle_error("read");
429 printf("Parent read %"PRIu64" (%#"PRIx64") from efd\en", u, u);
433 handle_error("fork");
445 .BR timerfd_create (2),