.\" Copyright (C) 2008 Michael Kerrisk .\" .\" %%%LICENSE_START(GPLv2+_SW_3_PARA) .\" This program is free software; you can redistribute it and/or modify .\" it under the terms of the GNU General Public License as published by .\" the Free Software Foundation; either version 2 of the License, or .\" (at your option) any later version. .\" .\" This program is distributed in the hope that it will be useful, .\" but WITHOUT ANY WARRANTY; without even the implied warranty of .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the .\" GNU General Public License for more details. .\" .\" You should have received a copy of the GNU General Public .\" License along with this manual; if not, see .\" . .\" %%%LICENSE_END .\" .\" FIXME Linux 3.0: timerfd_settime() adds a TFD_TIMER_CANCEL_ON_SET flag; .\" This flag needs to documented. .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2008 Akihiro MOTOKI .\" all rights reserved. .\" Translated 2008-11-19, Akihiro MOTOKI , LDP v3.13 .\" Updated 2009-04-24, Akihiro MOTOKI , LDP 3.20 .\" Updated 2012-05-04, Akihiro MOTOKI .\" .TH TIMERFD_CREATE 2 2011\-09\-14 Linux "Linux Programmer's Manual" .SH 名前 timerfd_create, timerfd_settime, timerfd_gettime \- ファイルディスクリプター経由で通知するタイマー .SH 書式 .nf \fB#include \fP .sp \fBint timerfd_create(int \fP\fIclockid\fP\fB, int \fP\fIflags\fP\fB);\fP .sp \fBint timerfd_settime(int \fP\fIfd\fP\fB, int \fP\fIflags\fP\fB,\fP \fB const struct itimerspec *\fP\fInew_value\fP\fB,\fP \fB struct itimerspec *\fP\fIold_value\fP\fB);\fP .sp \fBint timerfd_gettime(int \fP\fIfd\fP\fB, struct itimerspec *\fP\fIcurr_value\fP\fB);\fP .fi .SH 説明 これらのシステムコールは、満了通知をファイルディスクリプター経由で配送する タイマーの生成と操作を行う。 これらは、 \fBsetitimer\fP(2) や \fBtimer_create\fP(2) を用いる方法の代わりとなるものであり、このファイルディスクリプターを \fBselect\fP(2), \fBpoll\fP(2), \fBepoll\fP(7) で監視できるという利点がある。 .\" これらのシステムコールを使うのは、それぞれ \fBtimer_create\fP(2), \fBtimer_settime\fP(2), \fBtimer_gettime\fP(2) を使うのと同様である (\fBtimer_getoverrun\fP(2) に対応するものはなく、以下で説明するように この機能は \fBread\fP(2) により提供される)。 .SS timerfd_create() .\" FIXME .\" Additional clocks are now supported: .\" CLOCK_REALTIME_ALARM and CLOCK_BOOTTIME_ALARM .\" Since Linux 3.11, commit 11ffa9d6065f344a9bd769a2452f26f2f671e5f8 .\" CLOCK_BOOTTIME .\" Since Linux 3.15, commit 4a2378a943f09907fb1ae35c15de917f60289c14 \fBtimerfd_create\fP() は新規のタイマーオブジェクトを生成し、そのタイマーを参照するファイル ディスクリプターを返す。 \fIclockid\fP 引き数は、タイマーの進捗を管理するためのクロックを指定するもので、 \fBCLOCK_REALTIME\fP か \fBCLOCK_MONOTONIC\fP のいずれかでなければならない。 \fBCLOCK_REALTIME\fP はシステム全体で使用されるクロックで、このクロックは変更可能である。 \fBCLOCK_MONOTONIC\fP は変更されることのないクロックで、(システム時刻の手動での変更などの) システムクロックの不連続な変化の影響を受けない。 これらのクロックの現在の値は \fBclock_gettime\fP(2) を使って取得できる。 Linux 2.6.27 以降では、 以下の値のいくつかをビット単位の論理和 (OR) で指定することで、 \fBtimerfd_create\fP() の振舞いを変更することができる。 .TP 14 \fBTFD_NONBLOCK\fP 新しく生成されるオープンファイル記述 (open file description) の \fBO_NONBLOCK\fP ファイルステータスフラグをセットする。 このフラグを使うことで、 \fBO_NONBLOCK\fP をセットするために \fBfcntl\fP(2) を追加で呼び出す必要がなくなる。 .TP \fBTFD_CLOEXEC\fP 新しいファイルディスクリプターに対して close\-on\-exec (\fBFD_CLOEXEC\fP) フラグをセットする。 このフラグが役に立つ理由については、 \fBopen\fP(2) の \fBO_CLOEXEC\fP フラグの説明を参照のこと。 .PP バージョン 2.6.26 以前の Linux では、 \fIflags\fP 引き数は未使用であり、0 を指定しなければならない。 .SS timerfd_settime() \fBtimerfd_settime\fP() は、ファイルディスクリプター \fIfd\fP により参照されるタイマーを開始したり停止したりする。 \fInew_value\fP 引き数は、タイマーの満了時間 (expiration) の初期値と間隔 (interval) を 指定する。この引き数で使用されている \fIitimerspec\fP 構造体には 2 つのフィールドがあり、各フィールドは \fItimespec\fP 型の構造体である。 .in +4n .nf struct timespec { time_t tv_sec; /* Seconds */ long tv_nsec; /* Nanoseconds */ }; struct itimerspec { struct timespec it_interval; /* Interval for periodic timer */ struct timespec it_value; /* Initial expiration */ }; .fi .in .PP \fInew_value.it_value\fP はタイマーの満了時間の初期値を、秒とナノ秒で指定する。 \fInew_value.it_value\fP のフィールドのうち少なくとも一方に 0 以外の値を設定すると、 タイマーが開始される。 両方のフィールドに 0 を設定すると、タイマーが停止する。 \fInew_value.it_interval\fP はタイマーの一回目の満了後に繰り返しタイマーの満了間隔を、秒とナノ秒で指定する。 \fInew_value.it_interval\fP のフィールドのうち少なくとも一方に 0 以外の値を設定すると、 繰り返しタイマーが有効になる。 両方のフィールドに 0 を設定した場合、タイマーは \fInew_value.it_value\fP で指定された時間後に、一回だけ満了して停止する。 \fIflags\fP 引き数には 0 か \fBTFD_TIMER_ABSTIME\fP を指定する。 0 は相対時刻タイマーを意味し、 \fInew_value.it_value\fP では \fIclockid\fP で指定されたクロックの現在の値からの相対的な時刻を指定する。 \fBTFD_TIMER_ABSTIME\fP は絶対時刻タイマーを意味し、 \fInew_value.it_interval\fP では \fIclockid\fP で指定されたクロックの絶対時刻を指定する。 つまり、クロックの値が \fInew_value.it_interval\fP で指定された時刻に達したら、タイマーが満了する。 .\" \fIold_value\fP 引き数が NULL でない場合、 \fIold_value\fP 引き数が指す \fIitimerspec\fP 構造体は、 \fBtimerfd_settime\fP() を呼び出した時点でのタイマーの設定を返すのに使用される。 下記の \fBtimerfd_gettime\fP() の説明を参照。 .SS timerfd_gettime() \fBtimerfd_gettime\fP() は、ファイルディスクリプター \fIfd\fP で参照されるタイマーの現在の設定が入った \fIitimerspec\fP 構造体を、 \fIcurr_value\fP に格納して返す。 \fIit_value\fP フィールドは、タイマーが次に満了するまでの残り時間を返す。 この構造体の両方のフィールドが 0 であれば、タイマーは現在停止している。 タイマー設定時に \fBTFD_TIMER_ABSTIME\fP フラグが指定されたかに関わらず、このフィールドは常に相対値が格納される。 \fIit_interval\fP フィールドは、タイマーの間隔を返す。 この構造体の両方のフィールドが 0 であれば、タイマーは \fInew_value.it_value\fP で指定された時間後に一回だけ満了して停止するように設定されている。 .SS タイマーファイルディスクリプターに対する操作 \fBtimerfd_create\fP() が返すファイルディスクリプターは以下の操作をサポートしている。 .TP \fBread\fP(2) \fBtimerfd_settime\fP() を使ってタイマーの設定が最後変更されて以降、または \fBread\fP(2) の呼び出しに最後に成功して以降に、タイマーの満了が一回以上発生していれば、 \fBread\fP(2) に渡されたバッファーに、タイマー満了回数を示す 8 バイトの unsigned 型の整数 (\fIuint64_t\fP) が返される (返される値はホストバイトオーダ、つまりそのホストマシンにおける 整数の通常のバイトオーダである)。 .IP \fBread\fP(2) を行った時点でタイマーの満了が発生していなければ、 \fBread\fP(2) は停止 (block) する、もしくはファイルディスクリプターが 非停止 (nonblocking) に設定されている場合はエラー \fBEAGAIN\fP で失敗する (非停止モードにするには、 \fBfcntl\fP(2) の \fBF_SETFL\fP 命令で \fBO_NONBLOCK\fP フラグをセットする)。 .IP 渡されたバッファーの大きさが 8 バイト未満の場合、 \fBread\fP(2) はエラー \fBEINVAL\fP で失敗する。 .TP \fBpoll\fP(2), \fBselect\fP(2) (と同様の操作) 一つ以上のタイマー満了が発生していれば、 ファイルディスクリプターは読み出し可能となる (\fBselect\fP(2) の \fIreadfds\fP 引き数や \fBpoll\fP(2) の \fBPOLLIN\fP フラグ)。 .IP このファイルディスクリプターは、他のファイルディスクリプター多重 API である \fBpselect\fP(2), \fBppoll\fP(2), \fBepoll\fP(7) もサポートしている。 .TP \fBclose\fP(2) .\" ファイルディスクリプターがそれ以降は必要なくなった際には、クローズすべきである。 同じ timer オブジェクトに関連付けられたファイルディスクリプターが全て クローズされると、そのタイマーは解除され、 そのオブジェクト用の資源がカーネルにより解放される。 .SS "fork(2) での扱い" .\" \fBfork\fP(2) が行われると、子プロセスは \fBtimerfd_create\fP() により生成されたファイルディスクリプターのコピーを 継承する。そのファイルディスクリプターは、親プロセスの対応する ファイルディスクリプターと同じタイマーオブジェクトを参照しており、 子プロセスの \fBread\fP(2) でも同じタイマーの満了に関する情報が返される。 .SS "execve(2) での扱い" \fBexecve\fP(2) の前後で \fBtimerfd_create\fP() により生成されたファイルディスクリプターは保持され、 タイマーが開始されていた場合にはタイマーの満了が発生し続ける。 .SH 返り値 成功すると、 \fBtimerfd_create\fP() は新しいファイルディスクリプターを返す。 エラーの場合、\-1 を返し、 \fIerrno\fP にエラーを示す値を設定する。 \fBtimerfd_settime\fP() と \fBtimerfd_gettime\fP() は成功すると 0 を返す。 エラーの場合、\-1 を返し、 \fIerrno\fP にエラーを示す値を設定する。 .SH エラー \fBtimerfd_create\fP() は以下のエラーで失敗する可能性がある。 .TP \fBEINVAL\fP \fIclockid\fP 引き数が \fBCLOCK_MONOTONIC\fP でも \fBCLOCK_REALTIME\fP でもない。 .TP \fBEINVAL\fP \fIflags\fP が無効である。もしくは、Linux 2.6.26 以前の場合には \fIflags\fP が 0 以外である。 .TP \fBEMFILE\fP オープン済みのファイルディスクリプターの数がプロセスあたりの上限に 達していた。 .TP \fBENFILE\fP オープン済みのファイル総数がシステム全体の上限に達していた。 .TP \fBENODEV\fP (カーネル内の) 無名 inode デバイスをマウントできなかった。 .TP \fBENOMEM\fP タイマーを作成するのに十分なカーネルメモリーがなかった。 .PP \fBtimerfd_settime\fP() と \fBtimerfd_gettime\fP() は以下のエラーで失敗する可能性がある。 .TP \fBEBADF\fP \fIfd\fP が有効なファイルディスクリプターでない。 .TP \fBEFAULT\fP \fInew_value\fP, \fIold_value\fP, \fIcurr_value\fP が有効なポインターではない。 .TP \fBEINVAL\fP \fIfd\fP が有効な timerfd ファイルディスクリプターでない。 .PP \fBtimerfd_settime\fP() は以下のエラーで失敗することもある。 .TP \fBEINVAL\fP \fInew_value\fP が適切に初期化されていない (\fItv_nsec\fP の一つが 0 から 999,999,999 までの範囲に入っていない)。 .TP \fBEINVAL\fP .\" This case only checked since 2.6.29, and 2.2.2[78].some-stable-version. .\" In older kernel versions, no check was made for invalid flags. \fIflags\fP が無効である。 .SH バージョン これらのシステムコールはカーネル 2.6.25 以降の Linux で利用可能である。 ライブラリ側のサポートはバージョン 2.8 以降の glibc で提供されている。 .SH 準拠 これらのシステムコールは Linux 固有である。 .SH バグ .\" 2.6.29 現在のところ、 \fBtimerfd_create\fP() が対応している clockid の種類は \fBtimer_create\fP(2) よりも少ない。 .SH 例 以下のプログラムは、タイマーを作成し、その進捗をモニターするものである。 このプログラムは最大で 3 個のコマンドライン引き数を取り、 第一引き数ではタイマーの満了時間の初期値 (秒数単位) を、 第二引き数ではタイマーの間隔 (秒数単位) を、 第三引き数ではタイマーが何回満了したらプログラムが終了するかを指定する。 第二引き数と第三引き数は省略可能である。 以下のシェルのセッションはこのプログラムの使用例を示したものである。 .in +4n .nf $\fB a.out 3 1 100\fP 0.000: timer started 3.000: read: 1; total=1 4.000: read: 1; total=2 \fB^Z \fP # type control\-Z to suspend the program [1]+ Stopped ./timerfd3_demo 3 1 100 $ \fBfg\fP # Resume execution after a few seconds a.out 3 1 100 9.660: read: 5; total=7 10.000: read: 1; total=8 11.000: read: 1; total=9 \fB^C \fP # type control\-C to suspend the program .fi .in .SS プログラムのソース \& .nf .\" The commented out code here is what we currently need until .\" the required stuff is in glibc .\" .\" .\"/* Link with -lrt */ .\"#define _GNU_SOURCE .\"#include .\"#include .\"#include .\"#if defined(__i386__) .\"#define __NR_timerfd_create 322 .\"#define __NR_timerfd_settime 325 .\"#define __NR_timerfd_gettime 326 .\"#endif .\" .\"static int .\"timerfd_create(int clockid, int flags) .\"{ .\" return syscall(__NR_timerfd_create, clockid, flags); .\"} .\" .\"static int .\"timerfd_settime(int fd, int flags, struct itimerspec *new_value, .\" struct itimerspec *curr_value) .\"{ .\" return syscall(__NR_timerfd_settime, fd, flags, new_value, .\" curr_value); .\"} .\" .\"static int .\"timerfd_gettime(int fd, struct itimerspec *curr_value) .\"{ .\" return syscall(__NR_timerfd_gettime, fd, curr_value); .\"} .\" .\"#define TFD_TIMER_ABSTIME (1 << 0) .\" .\"//////////////////////////////////////////////////////////// #include #include #include #include #include #include /* Definition of uint64_t */ #define handle_error(msg) \e do { perror(msg); exit(EXIT_FAILURE); } while (0) static void print_elapsed_time(void) { static struct timespec start; struct timespec curr; static int first_call = 1; int secs, nsecs; if (first_call) { first_call = 0; if (clock_gettime(CLOCK_MONOTONIC, &start) == \-1) handle_error("clock_gettime"); } if (clock_gettime(CLOCK_MONOTONIC, &curr) == \-1) handle_error("clock_gettime"); secs = curr.tv_sec \- start.tv_sec; nsecs = curr.tv_nsec \- start.tv_nsec; if (nsecs < 0) { secs\-\-; nsecs += 1000000000; } printf("%d.%03d: ", secs, (nsecs + 500000) / 1000000); } int main(int argc, char *argv[]) { struct itimerspec new_value; int max_exp, fd; struct timespec now; uint64_t exp, tot_exp; ssize_t s; if ((argc != 2) && (argc != 4)) { fprintf(stderr, "%s init\-secs [interval\-secs max\-exp]\en", argv[0]); exit(EXIT_FAILURE); } if (clock_gettime(CLOCK_REALTIME, &now) == \-1) handle_error("clock_gettime"); /* Create a CLOCK_REALTIME absolute timer with initial expiration and interval as specified in command line */ new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]); new_value.it_value.tv_nsec = now.tv_nsec; if (argc == 2) { new_value.it_interval.tv_sec = 0; max_exp = 1; } else { new_value.it_interval.tv_sec = atoi(argv[2]); max_exp = atoi(argv[3]); } new_value.it_interval.tv_nsec = 0; fd = timerfd_create(CLOCK_REALTIME, 0); if (fd == \-1) handle_error("timerfd_create"); if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == \-1) handle_error("timerfd_settime"); print_elapsed_time(); printf("timer started\en"); for (tot_exp = 0; tot_exp < max_exp;) { s = read(fd, &exp, sizeof(uint64_t)); if (s != sizeof(uint64_t)) handle_error("read"); tot_exp += exp; print_elapsed_time(); printf("read: %llu; total=%llu\en", (unsigned long long) exp, (unsigned long long) tot_exp); } exit(EXIT_SUCCESS); } .fi .SH 関連項目 \fBeventfd\fP(2), \fBpoll\fP(2), \fBread\fP(2), \fBselect\fP(2), \fBsetitimer\fP(2), \fBsignalfd\fP(2), \fBtimer_create\fP(2), \fBtimer_gettime\fP(2), \fBtimer_settime\fP(2), \fBepoll\fP(7), \fBtime\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部である。 プロジェクトの説明とバグ報告に関する情報は \%http://www.kernel.org/doc/man\-pages/ に書かれている。