1 .\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" starting from a version by Davide Libenzi <davidel@xmailserver.org>
4 .\" This program is free software; you can redistribute it and/or modify
5 .\" it under the terms of the GNU General Public License as published by
6 .\" the Free Software Foundation; either version 2 of the License, or
7 .\" (at your option) any later version.
9 .\" This program is distributed in the hope that it will be useful,
10 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
11 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 .\" GNU General Public License for more details.
14 .\" You should have received a copy of the GNU General Public License
15 .\" along with this program; if not, write to the Free Software
16 .\" Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 .\" 2008-10-10, mtk: describe eventfd2(), and EFD_NONBLOCK and EFD_CLOEXEC
21 .TH EVENTFD 2 2009-01-26 Linux "Linux Programmer's Manual"
23 eventfd \- create a file descriptor for event notification
25 .B #include <sys/eventfd.h>
27 .BI "int eventfd(unsigned int " initval ", int " flags );
30 creates an "eventfd object" that can be used as
31 an event wait/notify mechanism by userspace applications,
32 and by the kernel to notify userspace applications of events.
33 The object contains an unsigned 64-bit integer
35 counter that is maintained by the kernel.
36 This counter is initialized with the value specified in the argument
39 Starting with Linux 2.6.27, the following values may be bitwise ORed in
41 to change the behaviour of
47 file status flag on the new open file description.
48 Using this flag saves extra calls to
50 to achieve the same result.
55 flag on the new file descriptor.
56 See the description of the
60 for reasons why this may be useful.
62 In Linux up to version 2.6.26, the
64 argument is unused, and must be specified as zero.
68 returns a new file descriptor that can be used to refer to the
70 The following operations can be performed on the file descriptor:
73 If the eventfd counter has a nonzero value, then a
75 returns 8 bytes containing that value,
76 and the counter's value is reset to zero.
77 (The returned value is in host byte order,
78 i.e., the native byte order for integers on the host machine.)
80 If the counter is zero at the time of the
82 then the call either blocks until the counter becomes nonzero,
83 or fails with the error
85 if the file descriptor has been made nonblocking.
89 will fail with the error
91 if the size of the supplied buffer is less than 8 bytes.
96 call adds the 8-byte integer value supplied in its
97 buffer to the counter.
98 The maximum value that may be stored in the counter is the largest
99 unsigned 64-bit value minus 1 (i.e., 0xfffffffffffffffe).
100 If the addition would cause the counter's value to exceed
101 the maximum, then the
103 either blocks until a
105 is performed on the file descriptor,
106 or fails with the error
108 if the file descriptor has been made nonblocking.
112 will fail with the error
114 if the size of the supplied buffer is less than 8 bytes,
115 or if an attempt is made to write the value 0xffffffffffffffff.
117 .BR poll "(2), " select "(2) (and similar)"
118 The returned file descriptor supports
127 The file descriptor is readable
135 if the counter has a value greater than 0.
137 The file descriptor is writable
145 if it is possible to write a value of at least "1" without blocking.
147 If an overflow of the counter value was detected,
150 indicates the file descriptor as being both readable and writable, and
157 can never overflow the counter.
158 However an overflow can occur if 2^64
159 eventfd "signal posts" were performed by the KAIO
160 subsystem (theoretically possible, but practically unlikely).
161 If an overflow has occurred, then
163 will return that maximum
165 value (i.e., 0xffffffffffffffff).
168 The eventfd file descriptor also supports the other file-descriptor
176 When the file descriptor is no longer required it should be closed.
177 When all file descriptors associated with the same eventfd object
178 have been closed, the resources for object are freed by the kernel.
180 A copy of the file descriptor created by
182 is inherited by the child produced by
184 The duplicate file descriptor is associated with the same
186 File descriptors created by
193 returns a new eventfd file descriptor.
194 On error, \-1 is returned and
196 is set to indicate the error.
202 or, in Linux 2.6.26 or earlier,
207 The per-process limit on open file descriptors has been reached.
210 The system-wide limit on the total number of open files has been
214 .\" Note from Davide:
215 .\" The ENODEV error is basically never going to happen if
216 .\" the kernel boots correctly. That error happen only if during
217 .\" the kernel initialization, some error occur in the anonymous
218 .\" inode source initialization.
219 Could not mount (internal) anonymous inode device.
222 There was insufficient memory to create a new
223 eventfd file descriptor.
226 is available on Linux since kernel 2.6.22.
227 Working support is provided in glibc since version 2.8.
228 .\" eventfd() is in glibc 2.7, but reportedly does not build
231 system call (see NOTES) is available on Linux since kernel 2.6.27.
232 Since version 2.9, the glibc
234 wrapper will employ the
236 system call, if it is supported by the kernel.
243 Applications can use an eventfd file descriptor instead of a pipe (see
245 in all cases where a pipe is used simply to signal events.
246 The kernel overhead of an eventfd file descriptor
247 is much lower than that of a pipe,
248 and only one file descriptor is
249 required (versus the two required for a pipe).
251 When used in the kernel, an eventfd
252 file descriptor can provide a kernel-userspace bridge allowing,
253 for example, functionalities like KAIO (kernel AIO)
254 .\" or eventually syslets/threadlets
255 to signal to a file descriptor that some operation is complete.
257 A key point about an eventfd file descriptor is that it can be
258 monitored just like any other file descriptor using
263 This means that an application can simultaneously monitor the
264 readiness of "traditional" files and the readiness of other
265 kernel mechanisms that support the eventfd interface.
268 interface, these mechanisms could not be multiplexed via
273 .SS Underlying Linux system calls
274 There are two underlying Linux system calls:
278 The former system call does not implement a
281 The latter system call implements the
283 values described above.
284 The glibc wrapper function will use
286 where it is available.
287 .SS Additional glibc features
288 The GNU C library defines an additional type,
289 and two functions that attempt to abstract some of the details of
290 reading and writing on an eventfd file descriptor:
294 typedef uint64_t eventfd_t;
296 int eventfd_read(int fd, eventfd_t *value);
297 int eventfd_write(int fd, eventfd_t value);
301 The functions perform the read and write operations on an
302 eventfd file descriptor,
303 returning 0 if the correct number of bytes was transferred,
307 The following program creates an eventfd file descriptor
308 and then forks to create a child process.
309 While the parent briefly sleeps,
310 the child writes each of the integers supplied in the program's
311 command-line arguments to the eventfd file descriptor.
312 When the parent has finished sleeping,
313 it reads from the eventfd file descriptor.
315 The following shell session shows a sample run of the program:
319 .RB "$" " ./a.out 1 2 4 7 14"
320 Child writing 1 to efd
321 Child writing 2 to efd
322 Child writing 4 to efd
323 Child writing 7 to efd
324 Child writing 14 to efd
325 Child completed write loop
327 Parent read 28 (0x1c) from efd
333 #include <sys/eventfd.h>
337 #include <stdint.h> /* Definition of uint64_t */
339 #define handle_error(msg) \\
340 do { perror(msg); exit(EXIT_FAILURE); } while (0)
343 main(int argc, char *argv[])
350 fprintf(stderr, "Usage: %s <num>...\\n", argv[0]);
356 handle_error("eventfd");
360 for (j = 1; j < argc; j++) {
361 printf("Child writing %s to efd\\n", argv[j]);
362 u = strtoull(argv[j], NULL, 0);
363 /* strtoull() allows various bases */
364 s = write(efd, &u, sizeof(uint64_t));
365 if (s != sizeof(uint64_t))
366 handle_error("write");
368 printf("Child completed write loop\\n");
375 printf("Parent about to read\\n");
376 s = read(efd, &u, sizeof(uint64_t));
377 if (s != sizeof(uint64_t))
378 handle_error("read");
379 printf("Parent read %llu (0x%llx) from efd\\n",
380 (unsigned long long) u, (unsigned long long) u);
384 handle_error("fork");
395 .BR timerfd_create (2),