1 .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" starting from a version by Davide Libenzi <davidel@xmailserver.org>
4 .\" %%%LICENSE_START(GPLv2+_SW_3_PARA)
5 .\" This program is free software; you can redistribute it and/or modify
6 .\" it under the terms of the GNU General Public License as published by
7 .\" the Free Software Foundation; either version 2 of the License, or
8 .\" (at your option) any later version.
10 .\" This program is distributed in the hope that it will be useful,
11 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
12 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 .\" GNU General Public License for more details.
15 .\" You should have received a copy of the GNU General Public
16 .\" License along with this manual; if not, see
17 .\" <http://www.gnu.org/licenses/>.
20 .\" 2008-10-10, mtk: describe eventfd2(), and EFD_NONBLOCK and EFD_CLOEXEC
22 .TH EVENTFD 2 2021-03-22 Linux "Linux Programmer's Manual"
24 eventfd \- create a file descriptor for event notification
27 .B #include <sys/eventfd.h>
29 .BI "int eventfd(unsigned int " initval ", int " flags );
33 creates an "eventfd object" that can be used as
34 an event wait/notify mechanism by user-space applications,
35 and by the kernel to notify user-space applications of events.
36 The object contains an unsigned 64-bit integer
38 counter that is maintained by the kernel.
39 This counter is initialized with the value specified in the argument
44 returns a new file descriptor that can be used to refer to the
47 The following values may be bitwise ORed in
49 to change the behavior of
52 .BR EFD_CLOEXEC " (since Linux 2.6.27)"
55 flag on the new file descriptor.
56 See the description of the
60 for reasons why this may be useful.
62 .BR EFD_NONBLOCK " (since Linux 2.6.27)"
65 file status flag on the open file description (see
67 referred to by the new file descriptor.
68 Using this flag saves extra calls to
70 to achieve the same result.
72 .BR EFD_SEMAPHORE " (since Linux 2.6.30)"
73 Provide semaphore-like semantics for reads from the new file descriptor.
76 In Linux up to version 2.6.26, the
78 argument is unused, and must be specified as zero.
80 The following operations can be performed on the file descriptor returned by
86 returns an 8-byte integer.
91 if the size of the supplied buffer is less than 8 bytes.
95 is in host byte order\(emthat is,
96 the native byte order for integers on the host machine.
100 depend on whether the eventfd counter currently has a nonzero value
103 flag was specified when creating the eventfd file descriptor:
108 was not specified and the eventfd counter has a nonzero value, then a
110 returns 8 bytes containing that value,
111 and the counter's value is reset to zero.
115 was specified and the eventfd counter has a nonzero value, then a
117 returns 8 bytes containing the value 1,
118 and the counter's value is decremented by 1.
120 If the eventfd counter is zero at the time of the call to
122 then the call either blocks until the counter becomes nonzero
125 proceeds as described above)
126 or fails with the error
128 if the file descriptor has been made nonblocking.
134 call adds the 8-byte integer value supplied in its
135 buffer to the counter.
136 The maximum value that may be stored in the counter is the largest
137 unsigned 64-bit value minus 1 (i.e., 0xfffffffffffffffe).
138 If the addition would cause the counter's value to exceed
139 the maximum, then the
141 either blocks until a
143 is performed on the file descriptor,
144 or fails with the error
146 if the file descriptor has been made nonblocking.
152 if the size of the supplied buffer is less than 8 bytes,
153 or if an attempt is made to write the value 0xffffffffffffffff.
155 .BR poll "(2), " select "(2) (and similar)"
156 The returned file descriptor supports
165 The file descriptor is readable
173 if the counter has a value greater than 0.
175 The file descriptor is writable
183 if it is possible to write a value of at least "1" without blocking.
185 If an overflow of the counter value was detected,
188 indicates the file descriptor as being both readable and writable, and
195 can never overflow the counter.
196 However an overflow can occur if 2^64
197 eventfd "signal posts" were performed by the KAIO
198 subsystem (theoretically possible, but practically unlikely).
199 If an overflow has occurred, then
201 will return that maximum
203 value (i.e., 0xffffffffffffffff).
206 The eventfd file descriptor also supports the other file-descriptor
213 When the file descriptor is no longer required it should be closed.
214 When all file descriptors associated with the same eventfd object
215 have been closed, the resources for object are freed by the kernel.
217 A copy of the file descriptor created by
219 is inherited by the child produced by
221 The duplicate file descriptor is associated with the same
223 File descriptors created by
227 unless the close-on-exec flag has been set.
231 returns a new eventfd file descriptor.
232 On error, \-1 is returned and
234 is set to indicate the error.
238 An unsupported value was specified in
242 The per-process limit on the number of open file descriptors has been reached.
245 The system-wide limit on the total number of open files has been
249 .\" Note from Davide:
250 .\" The ENODEV error is basically never going to happen if
251 .\" the kernel boots correctly. That error happen only if during
252 .\" the kernel initialization, some error occur in the anonymous
253 .\" inode source initialization.
254 Could not mount (internal) anonymous inode device.
257 There was insufficient memory to create a new
258 eventfd file descriptor.
261 is available on Linux since kernel 2.6.22.
262 Working support is provided in glibc since version 2.8.
263 .\" eventfd() is in glibc 2.7, but reportedly does not build
266 system call (see NOTES) is available on Linux since kernel 2.6.27.
267 Since version 2.9, the glibc
269 wrapper will employ the
271 system call, if it is supported by the kernel.
273 For an explanation of the terms used in this section, see
281 Interface Attribute Value
284 T} Thread safety MT-Safe
295 Applications can use an eventfd file descriptor instead of a pipe (see
297 in all cases where a pipe is used simply to signal events.
298 The kernel overhead of an eventfd file descriptor
299 is much lower than that of a pipe,
300 and only one file descriptor is
301 required (versus the two required for a pipe).
303 When used in the kernel, an eventfd
304 file descriptor can provide a bridge from kernel to user space, allowing,
305 for example, functionalities like KAIO (kernel AIO)
306 .\" or eventually syslets/threadlets
307 to signal to a file descriptor that some operation is complete.
309 A key point about an eventfd file descriptor is that it can be
310 monitored just like any other file descriptor using
315 This means that an application can simultaneously monitor the
316 readiness of "traditional" files and the readiness of other
317 kernel mechanisms that support the eventfd interface.
320 interface, these mechanisms could not be multiplexed via
326 The current value of an eventfd counter can be viewed
327 via the entry for the corresponding file descriptor in the process's
328 .IR /proc/[pid]/fdinfo
334 .SS C library/kernel differences
335 There are two underlying Linux system calls:
339 The former system call does not implement a
342 The latter system call implements the
344 values described above.
345 The glibc wrapper function will use
347 where it is available.
348 .SS Additional glibc features
349 The GNU C library defines an additional type,
350 and two functions that attempt to abstract some of the details of
351 reading and writing on an eventfd file descriptor:
355 typedef uint64_t eventfd_t;
357 int eventfd_read(int fd, eventfd_t *value);
358 int eventfd_write(int fd, eventfd_t value);
362 The functions perform the read and write operations on an
363 eventfd file descriptor,
364 returning 0 if the correct number of bytes was transferred,
367 The following program creates an eventfd file descriptor
368 and then forks to create a child process.
369 While the parent briefly sleeps,
370 the child writes each of the integers supplied in the program's
371 command-line arguments to the eventfd file descriptor.
372 When the parent has finished sleeping,
373 it reads from the eventfd file descriptor.
375 The following shell session shows a sample run of the program:
379 .RB "$" " ./a.out 1 2 4 7 14"
380 Child writing 1 to efd
381 Child writing 2 to efd
382 Child writing 4 to efd
383 Child writing 7 to efd
384 Child writing 14 to efd
385 Child completed write loop
387 Parent read 28 (0x1c) from efd
393 #include <sys/eventfd.h>
395 #include <inttypes.h> /* Definition of PRIu64 & PRIx64 */
398 #include <stdint.h> /* Definition of uint64_t */
400 #define handle_error(msg) \e
401 do { perror(msg); exit(EXIT_FAILURE); } while (0)
404 main(int argc, char *argv[])
411 fprintf(stderr, "Usage: %s <num>...\en", argv[0]);
417 handle_error("eventfd");
421 for (int j = 1; j < argc; j++) {
422 printf("Child writing %s to efd\en", argv[j]);
423 u = strtoull(argv[j], NULL, 0);
424 /* strtoull() allows various bases */
425 s = write(efd, &u, sizeof(uint64_t));
426 if (s != sizeof(uint64_t))
427 handle_error("write");
429 printf("Child completed write loop\en");
436 printf("Parent about to read\en");
437 s = read(efd, &u, sizeof(uint64_t));
438 if (s != sizeof(uint64_t))
439 handle_error("read");
440 printf("Parent read %"PRIu64" (%#"PRIx64") from efd\en", u, u);
444 handle_error("fork");
455 .BR timerfd_create (2),