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 .\" Japanese Version Copyright (c) 2008 Akihiro MOTOKI
22 .\" all rights reserved.
23 .\" Translated 2008-04-08, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
24 .\" Updated 2008-11-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.13
26 .TH EVENTFD 2 2009-01-26 Linux "Linux Programmer's Manual"
29 .\"O eventfd \- create a file descriptor for event notification
30 eventfd \- イベント通知用のファイルディスクリプタを生成する
33 .B #include <sys/eventfd.h>
35 .BI "int eventfd(unsigned int " initval ", int " flags );
39 .\"O creates an "eventfd object" that can be used as
40 .\"O an event wait/notify mechanism by userspace applications,
41 .\"O and by the kernel to notify userspace applications of events.
42 .\"O The object contains an unsigned 64-bit integer
44 .\"O counter that is maintained by the kernel.
45 .\"O This counter is initialized with the value specified in the argument
48 は "eventfd オブジェクト" を生成する。
49 eventfd オブジェクトはユーザ空間アプリケーションがイベント待ち受け/通知用の
50 仕組みとして使うことができる。また、カーネルがユーザ空間アプリケーションに
51 イベントを通知するためにも使うことができる。
52 このオブジェクトには、unsigned の 64 ビット整数
54 型のカウンタが含まれており、このカウンタはカーネルにより管理される。
59 .\"O Starting with Linux 2.6.27, the following values may be bitwise ORed in
61 .\"O to change the behaviour of
64 以下の値のいくつかをビット単位の論理和 (OR) で指定することで、
71 .\"O file status flag on the new open file description.
72 .\"O Using this flag saves extra calls to
74 .\"O to achieve the same result.
75 新しく生成されるオープンファイル記述 (open file description) の
85 .\"O Set the close-on-exec
86 .\"O .RB ( FD_CLOEXEC )
87 .\"O flag on the new file descriptor.
88 .\"O See the description of the
92 .\"O for reasons why this may be useful.
103 .\"O In Linux up to version 2.6.26, the
105 .\"O argument is unused, and must be specified as zero.
106 バージョン 2.6.26 以前の Linux では、
108 引き数は未使用であり、0 を指定しなければならない。
110 .\"O As its return value,
112 .\"O returns a new file descriptor that can be used to refer to the
114 .\"O The following operations can be performed on the file descriptor:
116 は eventfd オブジェクトを参照するのに使用できる新しいファイルディスクリプタ
117 を返す。返されたファイルディスクリプタに対しては以下の操作を実行できる。
120 .\"O If the eventfd counter has a nonzero value, then a
122 .\"O returns 8 bytes containing that value,
123 .\"O and the counter's value is reset to zero.
124 .\"O (The returned value is in host byte order,
125 .\"O i.e., the native byte order for integers on the host machine.)
126 eventfd カウンタが 0 以外の値の場合、
128 はカウンタ値を格納した 8 バイトの値を返し、
131 ホストマシンで整数表現に本来使用されるバイトオーダで格納される)。
133 .\"O If the counter is zero at the time of the
135 .\"O then the call either blocks until the counter becomes nonzero,
136 .\"O or fails with the error
138 .\"O if the file descriptor has been made nonblocking.
142 はカウンタが 0 以外になるまで停止 (block) する、
143 もしくはファイルディスクリプタが非停止 (nonblocking)
150 .\"O will fail with the error
152 .\"O if the size of the supplied buffer is less than 8 bytes.
153 渡されたバッファの大きさが 8 バイト未満の場合、
162 .\"O call adds the 8-byte integer value supplied in its
163 .\"O buffer to the counter.
164 .\"O The maximum value that may be stored in the counter is the largest
165 .\"O unsigned 64-bit value minus 1 (i.e., 0xfffffffffffffffe).
167 は、引き数のバッファで渡された 8 バイトの整数値をカウンタに加算する。
168 カウンタに格納可能な最大値は unsigned の 64 ビット整数の最大値から
169 1 を引いた値 (すなわち 0xfffffffffffffffe) である。
170 .\"O If the addition would cause the counter's value to exceed
171 .\"O the maximum, then the
173 .\"O either blocks until a
175 .\"O is performed on the file descriptor,
176 .\"O or fails with the error
178 .\"O if the file descriptor has been made nonblocking.
179 加算を行うとカウンタ値が最大値を超過する場合には、
185 もしくはファイルディスクリプタが非停止 (nonblocking)
192 .\"O will fail with the error
194 .\"O if the size of the supplied buffer is less than 8 bytes,
195 .\"O or if an attempt is made to write the value 0xffffffffffffffff.
196 渡されたバッファの大きさが 8 バイト未満の場合、もしくは
197 値 0xffffffffffffffff を書き込もうとした場合、
203 .\"O .BR poll "(2), " select "(2) (and similar)"
204 .BR poll "(2), " select "(2) (と同様の操作)"
205 .\"O The returned file descriptor supports
207 .\"O (and analogously
217 をサポートしており、以下のような動作をする。
220 .\"O The file descriptor is readable
228 .\"O if the counter has a value greater than 0.
230 ファイルディスクリプタは読み出し可能となる
240 .\"O The file descriptor is writable
248 .\"O if it is possible to write a value of at least "1" without blocking.
249 少なくとも値 "1" を、停止 (block) を伴わずに書き込める場合、
250 ファイルディスクリプタは書き込み可能となる
260 .\"O If an overflow of the counter value was detected,
263 .\"O indicates the file descriptor as being both readable and writable, and
268 カウンタ値のオーバーフローが検出された場合、
270 はファイルディスクリプタは読み出し可能と書き込み可能の両方を通知し、
277 .\"O can never overflow the counter.
278 .\"O However an overflow can occur if 2^64
279 .\"O eventfd "signal posts" were performed by the KAIO
280 .\"O subsystem (theoretically possible, but practically unlikely).
281 .\"O If an overflow has occurred, then
283 .\"O will return that maximum
285 .\"O value (i.e., 0xffffffffffffffff).
288 でカウンタがオーバーフローすることは決してない。
289 しかしながら、 KAIO サブシステムによって 2^64 回の eventfd "signal posts" が
290 実行された場合にはオーバーフローが起こり得る
291 (理論的にはあり得るが、実用的にはあり得ない)。
296 の最大値 (すなわち 0xffffffffffffffff) を返す。
299 .\"O The eventfd file descriptor also supports the other file-descriptor
300 .\"O multiplexing APIs:
301 .\"O .BR pselect (2),
305 eventfd ファイルディスクリプタは、これ以外のファイルディスクリプタ
313 .\"O When the file descriptor is no longer required it should be closed.
314 .\"O When all file descriptors associated with the same eventfd object
315 .\"O have been closed, the resources for object are freed by the kernel.
316 ファイルディスクリプタがそれ以降は必要なくなった際には、クローズすべきである。
317 同じ eventfd オブジェクトに関連付けられたファイルディスクリプタが全て
318 クローズされると、そのオブジェクト用の資源がカーネルにより解放される。
320 .\"O A copy of the file descriptor created by
322 .\"O is inherited by the child produced by
324 .\"O The duplicate file descriptor is associated with the same
326 .\"O File descriptors created by
328 .\"O are preserved across
333 で生成されたファイルディスクリプタのコピーを継承する。
334 複製されたファイルディスクリプタは同じ eventfd オブジェクトに関連付けられる。
338 で生成されたファイルディスクリプタは保持される。
339 .\"O .SH "RETURN VALUE"
343 .\"O returns a new eventfd file descriptor.
344 .\"O On error, \-1 is returned and
346 .\"O is set to indicate the error.
349 は新規の eventfd ファイルディスクリプタを返す。
359 .\"O or, in Linux 2.6.26 or earlier,
369 .\"O The per-process limit on open file descriptors has been reached.
370 オープン済みのファイルディスクリプタの数がプロセスあたりの上限に
374 .\"O The system-wide limit on the total number of open files has been
376 オープン済みのファイル総数がシステム全体の上限に達していた。
379 .\" Note from Davide:
380 .\" The ENODEV error is basically never going to happen if
381 .\" the kernel boots correctly. That error happen only if during
382 .\" the kernel initialization, some error occur in the anonymous
383 .\" inode source initialization.
384 .\"O Could not mount (internal) anonymous inode device.
385 (カーネル内の) 無名 inode デバイスをマウントできなかった。
388 .\"O There was insufficient memory to create a new
389 .\"O eventfd file descriptor.
390 新しい eventfd ファイルディスクリプタを生成するのに十分なメモリがなかった。
394 .\"O is available on Linux since kernel 2.6.22.
395 .\"O Working support is provided in glibc since version 2.8.
397 はカーネル 2.6.22 以降の Linux で利用可能である。
398 正しく動作する glibc 側のサポートはバージョン 2.8 以降で提供されている。
399 .\" eventfd() is in glibc 2.7, but reportedly does not build
402 .\"O system call (see NOTES) is available on Linux since kernel 2.6.27.
405 カーネル 2.6.27 以降の Linux で利用可能である。
406 .\"O Since version 2.9, the glibc
408 .\"O wrapper will employ the
410 .\"O system call, if it is supported by the kernel.
411 バージョン 2.9 以降では、glibc の
413 のラッパー関数は、カーネルが対応していれば
416 .\"O .SH CONFORMING TO
421 .\"O are Linux-specific.
428 .\"O Applications can use an eventfd file descriptor instead of a pipe (see
430 .\"O in all cases where a pipe is used simply to signal events.
431 .\"O The kernel overhead of an eventfd file descriptor
432 .\"O is much lower than that of a pipe,
433 .\"O and only one file descriptor is
434 .\"O required (versus the two required for a pipe).
435 アプリケーションは、パイプをイベントを通知するためだけに使用している
436 全ての場面において、パイプの代わりに eventfd ファイルディスクリプタを
438 eventfd ファイルディスクリプタを使う方が、パイプを使う場合に比べて
439 カーネルでのオーバヘッドは比べるとずっと小さく、ファイルディスクリプタも
440 一つしか必要としない (パイプの場合は二つ必要である)。
442 .\"O When used in the kernel, an eventfd
443 .\"O file descriptor can provide a kernel-userspace bridge allowing,
444 .\"O for example, functionalities like KAIO (kernel AIO)
445 .\"O .\" or eventually syslets/threadlets
446 .\"O to signal to a file descriptor that some operation is complete.
447 カーネル内で使用すると、eventfd ファイルディスクリプタは
448 カーネル空間とユーザ空間のブリッジ機能を提供することができ、
449 例えば KAIO (kernel AIO)
450 .\" や eventually syslets/threadlets
451 のような機能が、あるファイルディスクリプタに何らかの操作が完了したことを
454 .\"O A key point about an eventfd file descriptor is that it can be
455 .\"O monitored just like any other file descriptor using
460 eventfd ファイルディスクリプタの重要な点は、
465 を使って他のファイルディスクリプタと全く同様に監視できる点である。
466 .\"O This means that an application can simultaneously monitor the
467 .\"O readiness of "traditional" files and the readiness of other
468 .\"O kernel mechanisms that support the eventfd interface.
471 .\"O interface, these mechanisms could not be multiplexed via
476 このことは、アプリケーションは「従来の (traditional)」 ファイルの状態変化と
477 eventfd インタフェースをサポートする他のカーネル機構の状態変化を同時に監視
480 インタフェースがない時には、これらのカーネル機構は
485 .\"O .SS Underlying Linux system calls
486 .SS 下層にある Linux のシステムコール
487 .\"O There are two underlying Linux system calls:
489 .\"O and the more recent
490 .\"O .BR eventfd2 ().
491 .\"O The former system call does not implement a
494 .\"O The latter system call implements the
496 .\"O values described above.
497 下層にある Linux システムコールは二種類あり、
510 .\"O The glibc wrapper function will use
512 .\"O where it is available.
516 .\"O .SS Additional glibc features
518 .\"O The GNU C library defines an additional type,
519 .\"O and two functions that attempt to abstract some of the details of
520 .\"O reading and writing on an eventfd file descriptor:
521 GNU C ライブラリは、eventfd ファイルディスクリプタの読み出しと書き込みに
522 を関する詳細のいくつか抽象化するために、一つの型と、二つの関数を追加で
527 typedef uint64_t eventfd_t;
529 int eventfd_read(int fd, eventfd_t *value);
530 int eventfd_write(int fd, eventfd_t value);
534 .\"O The functions perform the read and write operations on an
535 .\"O eventfd file descriptor,
536 .\"O returning 0 if the correct number of bytes was transferred,
537 .\"O or \-1 otherwise.
538 これらの関数は、eventfd ファイルディスクリプタに対する読み出しと
539 書き込みの操作を実行し、正しいバイト数が転送された場合には
540 0 を返し、そうでない場合は \-1 を返す。
544 .\"O The following program creates an eventfd file descriptor
545 .\"O and then forks to create a child process.
546 .\"O While the parent briefly sleeps,
547 .\"O the child writes each of the integers supplied in the program's
548 .\"O command-line arguments to the eventfd file descriptor.
549 .\"O When the parent has finished sleeping,
550 .\"O it reads from the eventfd file descriptor.
551 以下のプログラムは eventfd ファイルディスクリプタを生成し、
552 その後 fork を実行して子プロセスを生成する。
553 親プロセスが少しの間 sleep する間に、子プロセスは
554 プログラムのコマンドライン引き数で指定された整数(列)をそれぞれ
555 eventfd ファイルディスクリプタに書き込む。
556 親プロセスは sleep を完了すると eventfd ファイルディスクリプタから
559 .\"O The following shell session shows a sample run of the program:
560 以下に示すシェルセッションにこのプログラムの使い方を示す。
564 .RB "$" " ./a.out 1 2 4 7 14"
565 Child writing 1 to efd
566 Child writing 2 to efd
567 Child writing 4 to efd
568 Child writing 7 to efd
569 Child writing 14 to efd
570 Child completed write loop
572 Parent read 28 (0x1c) from efd
575 .\"O .SS Program source
579 #include <sys/eventfd.h>
583 #include <stdint.h> /* Definition of uint64_t */
585 #define handle_error(msg) \\
586 do { perror(msg); exit(EXIT_FAILURE); } while (0)
589 main(int argc, char *argv[])
596 fprintf(stderr, "Usage: %s <num>...\\n", argv[0]);
602 handle_error("eventfd");
606 for (j = 1; j < argc; j++) {
607 printf("Child writing %s to efd\\n", argv[j]);
608 u = strtoull(argv[j], NULL, 0);
609 /* strtoull() allows various bases */
610 s = write(efd, &u, sizeof(uint64_t));
611 if (s != sizeof(uint64_t))
612 handle_error("write");
614 printf("Child completed write loop\\n");
621 printf("Parent about to read\\n");
622 s = read(efd, &u, sizeof(uint64_t));
623 if (s != sizeof(uint64_t))
624 handle_error("read");
625 printf("Parent read %llu (0x%llx) from efd\\n",
626 (unsigned long long) u, (unsigned long long) u);
630 handle_error("fork");
642 .BR timerfd_create (2),