]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man2/eventfd.2
man/: EXAMPLES: Add missing includes
[thirdparty/man-pages.git] / man / man2 / eventfd.2
CommitLineData
a1eaacb1 1'\" t
8b428090
MK
2.\" Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
3.\" starting from a version by Davide Libenzi <davidel@xmailserver.org>
4.\"
e4a74ca8 5.\" SPDX-License-Identifier: GPL-2.0-or-later
8b428090 6.\"
c22cb204
MK
7.\" 2008-10-10, mtk: describe eventfd2(), and EFD_NONBLOCK and EFD_CLOEXEC
8.\"
4c1c5274 9.TH eventfd 2 (date) "Linux man-pages (unreleased)"
8b428090
MK
10.SH NAME
11eventfd \- create a file descriptor for event notification
f8b67e8e
AC
12.SH LIBRARY
13Standard C library
8fc3b2cf 14.RI ( libc ", " \-lc )
8b428090 15.SH SYNOPSIS
c7db92b9 16.nf
8b428090 17.B #include <sys/eventfd.h>
c6d039a3 18.P
8b428090 19.BI "int eventfd(unsigned int " initval ", int " flags );
c7db92b9 20.fi
8b428090
MK
21.SH DESCRIPTION
22.BR eventfd ()
23creates an "eventfd object" that can be used as
7fac88a9
MK
24an event wait/notify mechanism by user-space applications,
25and by the kernel to notify user-space applications of events.
8b428090
MK
26The object contains an unsigned 64-bit integer
27.RI ( uint64_t )
28counter that is maintained by the kernel.
29This counter is initialized with the value specified in the argument
30.IR initval .
c6d039a3 31.P
735e2912
MK
32As its return value,
33.BR eventfd ()
34returns a new file descriptor that can be used to refer to the
35eventfd object.
c6d039a3 36.P
0986cb57 37The following values may be bitwise ORed in
1ae6b2c7 38.I flags
a1fa36af 39to change the behavior of
e64b5482 40.BR eventfd ():
e64b5482 41.TP
0986cb57 42.BR EFD_CLOEXEC " (since Linux 2.6.27)"
e64b5482
MK
43Set the close-on-exec
44.RB ( FD_CLOEXEC )
45flag on the new file descriptor.
c5571b61 46See the description of the
e64b5482
MK
47.B O_CLOEXEC
48flag in
49.BR open (2)
50for reasons why this may be useful.
4eb6d333 51.TP
0986cb57 52.BR EFD_NONBLOCK " (since Linux 2.6.27)"
4eb6d333 53Set the
1ae6b2c7 54.B O_NONBLOCK
7f11e32c
MK
55file status flag on the open file description (see
56.BR open (2))
57referred to by the new file descriptor.
4eb6d333
MK
58Using this flag saves extra calls to
59.BR fcntl (2)
60to achieve the same result.
0986cb57
MK
61.TP
62.BR EFD_SEMAPHORE " (since Linux 2.6.30)"
63Provide semaphore-like semantics for reads from the new file descriptor.
64See below.
c6d039a3 65.P
b324e17d 66Up to Linux 2.6.26, the
8b428090 67.I flags
e64b5482 68argument is unused, and must be specified as zero.
c6d039a3 69.P
735e2912
MK
70The following operations can be performed on the file descriptor returned by
71.BR eventfd ():
8b428090
MK
72.TP
73.BR read (2)
0986cb57
MK
74Each successful
75.BR read (2)
76returns an 8-byte integer.
77A
78.BR read (2)
26cd31fd 79fails with the error
0986cb57
MK
80.B EINVAL
81if the size of the supplied buffer is less than 8 bytes.
82.IP
83The value returned by
84.BR read (2)
36546c38 85is in host byte order\[em]that is,
88879aeb 86the native byte order for integers on the host machine.
0986cb57
MK
87.IP
88The semantics of
89.BR read (2)
90depend on whether the eventfd counter currently has a nonzero value
91and whether the
1ae6b2c7 92.B EFD_SEMAPHORE
0986cb57
MK
93flag was specified when creating the eventfd file descriptor:
94.RS
cdede5cd 95.IP \[bu] 3
0986cb57 96If
1ae6b2c7 97.B EFD_SEMAPHORE
0986cb57 98was not specified and the eventfd counter has a nonzero value, then a
8b428090
MK
99.BR read (2)
100returns 8 bytes containing that value,
101and the counter's value is reset to zero.
cdede5cd 102.IP \[bu]
0986cb57 103If
1ae6b2c7 104.B EFD_SEMAPHORE
0986cb57
MK
105was specified and the eventfd counter has a nonzero value, then a
106.BR read (2)
107returns 8 bytes containing the value 1,
108and the counter's value is decremented by 1.
cdede5cd 109.IP \[bu]
0986cb57 110If the eventfd counter is zero at the time of the call to
8b428090 111.BR read (2),
0986cb57
MK
112then the call either blocks until the counter becomes nonzero
113(at which time, the
114.BR read (2)
115proceeds as described above)
8b428090
MK
116or fails with the error
117.B EAGAIN
ff40dbb3 118if the file descriptor has been made nonblocking.
0986cb57 119.RE
8b428090
MK
120.TP
121.BR write (2)
122A
123.BR write (2)
124call adds the 8-byte integer value supplied in its
125buffer to the counter.
126The maximum value that may be stored in the counter is the largest
127unsigned 64-bit value minus 1 (i.e., 0xfffffffffffffffe).
128If the addition would cause the counter's value to exceed
129the maximum, then the
130.BR write (2)
131either blocks until a
132.BR read (2)
133is performed on the file descriptor,
134or fails with the error
135.B EAGAIN
ff40dbb3 136if the file descriptor has been made nonblocking.
8b428090
MK
137.IP
138A
139.BR write (2)
26cd31fd 140fails with the error
8b428090
MK
141.B EINVAL
142if the size of the supplied buffer is less than 8 bytes,
143or if an attempt is made to write the value 0xffffffffffffffff.
144.TP
6fdb1c03
AC
145.BR poll (2)
146.TQ
147.BR select (2)
148.TQ
149(and similar)
8b428090
MK
150The returned file descriptor supports
151.BR poll (2)
152(and analogously
153.BR epoll (7))
154and
155.BR select (2),
156as follows:
157.RS
cdede5cd 158.IP \[bu] 3
8b428090
MK
159The file descriptor is readable
160(the
161.BR select (2)
162.I readfds
163argument; the
164.BR poll (2)
165.B POLLIN
166flag)
167if the counter has a value greater than 0.
cdede5cd 168.IP \[bu]
8b428090
MK
169The file descriptor is writable
170(the
171.BR select (2)
172.I writefds
173argument; the
174.BR poll (2)
175.B POLLOUT
176flag)
177if it is possible to write a value of at least "1" without blocking.
cdede5cd 178.IP \[bu]
20e5fb78
MK
179If an overflow of the counter value was detected,
180then
8b428090 181.BR select (2)
20e5fb78 182indicates the file descriptor as being both readable and writable, and
8b428090 183.BR poll (2)
20e5fb78 184returns a
8b428090 185.B POLLERR
20e5fb78 186event.
8b428090
MK
187As noted above,
188.BR write (2)
189can never overflow the counter.
e4b01162 190However an overflow can occur if 2\[ha]64
8b428090
MK
191eventfd "signal posts" were performed by the KAIO
192subsystem (theoretically possible, but practically unlikely).
193If an overflow has occurred, then
194.BR read (2)
195will return that maximum
196.I uint64_t
197value (i.e., 0xffffffffffffffff).
198.RE
199.IP
200The eventfd file descriptor also supports the other file-descriptor
201multiplexing APIs:
531b15bc 202.BR pselect (2)
8b428090 203and
531b15bc 204.BR ppoll (2).
8b428090
MK
205.TP
206.BR close (2)
207When the file descriptor is no longer required it should be closed.
208When all file descriptors associated with the same eventfd object
209have been closed, the resources for object are freed by the kernel.
c6d039a3 210.P
8b428090
MK
211A copy of the file descriptor created by
212.BR eventfd ()
213is inherited by the child produced by
214.BR fork (2).
215The duplicate file descriptor is associated with the same
216eventfd object.
217File descriptors created by
218.BR eventfd ()
219are preserved across
d45105d9
AS
220.BR execve (2),
221unless the close-on-exec flag has been set.
47297adb 222.SH RETURN VALUE
8b428090
MK
223On success,
224.BR eventfd ()
225returns a new eventfd file descriptor.
226On error, \-1 is returned and
227.I errno
228is set to indicate the error.
229.SH ERRORS
40725279
MK
230.TP
231.B EINVAL
0986cb57
MK
232An unsupported value was specified in
233.IR flags .
8b428090
MK
234.TP
235.B EMFILE
26c32fab 236The per-process limit on the number of open file descriptors has been reached.
8b428090
MK
237.TP
238.B ENFILE
239The system-wide limit on the total number of open files has been
240reached.
241.TP
242.B ENODEV
243.\" Note from Davide:
244.\" The ENODEV error is basically never going to happen if
245.\" the kernel boots correctly. That error happen only if during
246.\" the kernel initialization, some error occur in the anonymous
247.\" inode source initialization.
248Could not mount (internal) anonymous inode device.
249.TP
250.B ENOMEM
251There was insufficient memory to create a new
252eventfd file descriptor.
288c1a09
ZL
253.SH ATTRIBUTES
254For an explanation of the terms used in this section, see
255.BR attributes (7).
256.TS
257allbox;
c466875e 258lbx lb lb
288c1a09
ZL
259l l l.
260Interface Attribute Value
261T{
9e54434e
BR
262.na
263.nh
288c1a09
ZL
264.BR eventfd ()
265T} Thread safety MT-Safe
266.TE
196224f7 267.SH VERSIONS
4131356c
AC
268.SS C library/kernel differences
269There are two underlying Linux system calls:
270.BR eventfd ()
271and the more recent
272.BR eventfd2 ().
273The former system call does not implement a
274.I flags
275argument.
276The latter system call implements the
277.I flags
278values described above.
279The glibc wrapper function will use
280.BR eventfd2 ()
281where it is available.
282.SS Additional glibc features
283The GNU C library defines an additional type,
284and two functions that attempt to abstract some of the details of
285reading and writing on an eventfd file descriptor:
c6d039a3 286.P
4131356c
AC
287.in +4n
288.EX
289typedef uint64_t eventfd_t;
fe5dba13 290\&
4131356c
AC
291int eventfd_read(int fd, eventfd_t *value);
292int eventfd_write(int fd, eventfd_t value);
293.EE
294.in
c6d039a3 295.P
4131356c
AC
296The functions perform the read and write operations on an
297eventfd file descriptor,
298returning 0 if the correct number of bytes was transferred,
299or \-1 otherwise.
300.SH STANDARDS
301Linux, GNU.
302.SH HISTORY
303.TP
196224f7 304.BR eventfd ()
4131356c
AC
305Linux 2.6.22,
306glibc 2.8.
196224f7 307.\" eventfd() is in glibc 2.7, but reportedly does not build
4131356c 308.TP
196224f7 309.BR eventfd2 ()
4131356c 310Linux 2.6.27 (see VERSIONS).
196224f7
AC
311Since glibc 2.9, the
312.BR eventfd ()
313wrapper will employ the
314.BR eventfd2 ()
315system call, if it is supported by the kernel.
8b428090
MK
316.SH NOTES
317Applications can use an eventfd file descriptor instead of a pipe (see
318.BR pipe (2))
319in all cases where a pipe is used simply to signal events.
320The kernel overhead of an eventfd file descriptor
321is much lower than that of a pipe,
322and only one file descriptor is
323required (versus the two required for a pipe).
c6d039a3 324.P
8b428090 325When used in the kernel, an eventfd
7fac88a9 326file descriptor can provide a bridge from kernel to user space, allowing,
8b428090
MK
327for example, functionalities like KAIO (kernel AIO)
328.\" or eventually syslets/threadlets
329to signal to a file descriptor that some operation is complete.
c6d039a3 330.P
8b428090
MK
331A key point about an eventfd file descriptor is that it can be
332monitored just like any other file descriptor using
333.BR select (2),
334.BR poll (2),
335or
336.BR epoll (7).
337This means that an application can simultaneously monitor the
338readiness of "traditional" files and the readiness of other
339kernel mechanisms that support the eventfd interface.
340(Without the
341.BR eventfd ()
342interface, these mechanisms could not be multiplexed via
343.BR select (2),
344.BR poll (2),
345or
346.BR epoll (7).)
c6d039a3 347.P
9764a9ff
MK
348The current value of an eventfd counter can be viewed
349via the entry for the corresponding file descriptor in the process's
1ae6b2c7 350.IR /proc/ pid /fdinfo
9764a9ff
MK
351directory.
352See
353.BR proc (5)
354for further details.
a14af333 355.SH EXAMPLES
8b428090
MK
356The following program creates an eventfd file descriptor
357and then forks to create a child process.
358While the parent briefly sleeps,
359the child writes each of the integers supplied in the program's
360command-line arguments to the eventfd file descriptor.
361When the parent has finished sleeping,
362it reads from the eventfd file descriptor.
c6d039a3 363.P
8b428090 364The following shell session shows a sample run of the program:
c6d039a3 365.P
8b428090 366.in +4n
0ffeaeae 367.EX
b43a3b30 368.RB "$" " ./a.out 1 2 4 7 14"
8b428090
MK
369Child writing 1 to efd
370Child writing 2 to efd
371Child writing 4 to efd
372Child writing 7 to efd
373Child writing 14 to efd
374Child completed write loop
375Parent about to read
376Parent read 28 (0x1c) from efd
0ffeaeae 377.EE
1c32ee47 378.in
9c330504 379.SS Program source
d84d0300 380\&
33857069 381.\" SRC BEGIN (eventfd.c)
e7d0bb47 382.EX
5a5208c1 383#include <err.h>
47b94bbd
AC
384#include <inttypes.h>
385#include <stdio.h>
386#include <stdlib.h>
8b428090 387#include <sys/eventfd.h>
0320049e 388#include <sys/types.h>
8b428090 389#include <unistd.h>
fe5dba13 390\&
8b428090
MK
391int
392main(int argc, char *argv[])
393{
0b94bd78
AC
394 int efd;
395 uint64_t u;
396 ssize_t s;
fe5dba13 397\&
8b428090 398 if (argc < 2) {
d1a71985 399 fprintf(stderr, "Usage: %s <num>...\en", argv[0]);
8b428090
MK
400 exit(EXIT_FAILURE);
401 }
fe5dba13 402\&
8b428090
MK
403 efd = eventfd(0, 0);
404 if (efd == \-1)
5a5208c1 405 err(EXIT_FAILURE, "eventfd");
fe5dba13 406\&
8b428090
MK
407 switch (fork()) {
408 case 0:
b42296e4 409 for (size_t j = 1; j < argc; j++) {
d1a71985 410 printf("Child writing %s to efd\en", argv[j]);
8b428090
MK
411 u = strtoull(argv[j], NULL, 0);
412 /* strtoull() allows various bases */
413 s = write(efd, &u, sizeof(uint64_t));
414 if (s != sizeof(uint64_t))
5a5208c1 415 err(EXIT_FAILURE, "write");
8b428090 416 }
d1a71985 417 printf("Child completed write loop\en");
fe5dba13 418\&
8b428090 419 exit(EXIT_SUCCESS);
fe5dba13 420\&
8b428090
MK
421 default:
422 sleep(2);
fe5dba13 423\&
d1a71985 424 printf("Parent about to read\en");
8b428090
MK
425 s = read(efd, &u, sizeof(uint64_t));
426 if (s != sizeof(uint64_t))
5a5208c1 427 err(EXIT_FAILURE, "read");
dc97703b 428 printf("Parent read %"PRIu64" (%#"PRIx64") from efd\en", u, u);
8b428090 429 exit(EXIT_SUCCESS);
fe5dba13 430\&
8b428090 431 case \-1:
5a5208c1 432 err(EXIT_FAILURE, "fork");
8b428090
MK
433 }
434}
e7d0bb47 435.EE
33857069 436.\" SRC END
47297adb 437.SH SEE ALSO
8b428090
MK
438.BR futex (2),
439.BR pipe (2),
440.BR poll (2),
441.BR read (2),
442.BR select (2),
443.BR signalfd (2),
444.BR timerfd_create (2),
445.BR write (2),
446.BR epoll (7),
447.BR sem_overview (7)