getitimerのヘルプ・マニュアル
日本語 英語
getitimer --help
man getitimer
GETITIMER(2) Linux Programmer’s Manual GETITIMER(2)
名前
getitimer, setitimer - インターバル・タイマーの値を取得または設定する
書式
#include
int getitimer(int which, struct itimerval *curr_value);
int setitimer(int which, const struct itimerval *new_value,
struct itimerval *old_value);
説明
シ ステムは 1 個のプロセスにつき 3 個のインターバル・タイマーを提供する
。それぞれのタイマーは別々の時間領域で減少する。どのタイマーも満了す る
と プロセスにシグナルが送られ、タイマーは (設定によっては) 再び開始され
る。
ITIMER_REAL 実時間 (real time) で減少し、満了すると SIGALRM が送られ
る。
ITIMER_VIRTUAL プ ロセスが実行されている間のみ減少し、満了すると SIGV-
TALRM が送られる。
ITIMER_PROF プロセスが実行されていて、かつシステムがそのプロセスのた
めに処理を行なっている間に減少する。多くの場合、このタイ
マーは ITIMER_VIRTUAL と組み合わされて、アプリケーション
がカーネル空間とユーザー空間でどれだけの時間を過ごしたか
をプロファイルするのに使用される。満了すると SIGPROF が
送られる。
タイマーの値は以下の構造体によって定義される:
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
getitimer() 関 数 は 、 which で 指 定されたタイマー (ITIMER_REAL,
ITIMER_VIRTUAL, ITIMER_PROF のどれか) の現在の設定を、 curr_value で 指
定 された構造体に格納する。 it_value 要素にはタイマーの残り時間が設定さ
れる。タイマーがオフの場合はゼロが設定される。同様に it_interval には初
期値が設定される。
setitimer() 関 数 は 指 定されたタイマーに new_value の値を設定する。
old_value が NULL 以外の場合、タイマーの古い値が old_value に格納される
。
タ イ マ ー は it_value からゼロへ向けて減っていき、シグナルを生成し、
it_interval に初期化される。タイマーがゼロに設定された場合 (it_value が
ゼロか、タイマーが満了した時に it_interval がゼロの場合) は停止する。
タイマーの期間は tv_sec と tv_usec の両方により決定される。
要 求 した時間がくる前にタイマーが満了することはないが、逆にある (短い)
時間だけ満了が遅れることはある。どれだけ遅れるかはシステムの時間分解 能
とシステムの負荷に依存する (time(7) 参照; 但し、バグの項も参照のこと)。
タイマーが満了するとシグナルが生成され、タイマーは初期化される。プロ セ
スがアクティブな時 (ITIMER_VIRTUAL の場合には常にそうである) にタイマー
が満了した場合、生成されたシグナルはすぐに配送される。それ以外の場合 は
、システムの負荷により少しの時間だけ遅れて配送される。
返り値
成功した場合はゼロが返る。エラーの場合は -1 が返り、 errno が適切に設定
される。
エラー
EFAULT new_value, old_value, curr_value が有効なポインターでない。
EINVAL which が ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF のどれでもない
。 ま た は (Linux 2.6.22 以降で) new_value で指定された構造体の
tv_usec フィールドの一つが 0 以上 999999 以下の範囲に入らない 値
である。
準拠
POSIX.1-2001, SVr4, 4.4BSD ( こ のコールは 4.2BSD で始めて現われた).
POSIX.1-2008 では、 getitimer() と setitimer() は廃止予定とされており、
代 わ りに POSIX タイマー API (timer_gettime(2), timer_settime(2) など)
を使うことが推奨されている。
注意
fork(2) で作成された子プロセスは、親プロセスのインターバル・タイマー を
継承しない。 execve(2) の前後ではインターバル・タイマーは保存される。
POSIX.1 では、 setitimer() と、 alarm(2), sleep(3), usleep(3) という 3
つのインタフェースとの相互の影響については規定していない。
バグ
シグナルの生成と配送は別個のものであり、前述のシグナルのそれぞれにつ い
て 一 つ だ け が プ ロ セ スのために待機する。非常に重い負荷の下では、
ITIMER_REAL タイマーでは、時間切れにより生成された一つ前のシグナルが 配
送される前に、次の時間切れが起こる場合がある。そのような場合、 2 個めの
イベントに対するシグナルは失われてしまう。
バージョン 2.6.16 より前の Linux カーネルでは、タイマーの値は jiffy で
表現される。要求が jiffy 表現で (include/linux/jiffies.h で定義されてい
る) MAX_SEC_IN_JIFFIES を越える値をタイマーに設定しようとするものの場合
、タイマーは暗黙にこの上限値に切り詰められる。 Linux/i386 の場合 (Linux
2.6.13 以降では jiffy は 0.004 秒) の場合、これはタイマーの上限値がおよ
そ 99.42 日になることを意味する。 Linux 2.6.16 以降では、カーネルは時間
に関する内部表現として異なる表現を使うようになっており、この上限はな く
なった。
(i386 を含む) いくつかのシステムでは、バージョン 2.6.12 以前の Linux カ
ーネルはある種の状況では 1 jiffy 早くタイマーが終了してしまうというバグ
があった。このバグはカーネル 2.6.12 で修正された。
POSIX.1-2001 では setitimer() は tv_usec の値が 0 から 999999 の範囲外
である場合には失敗するべきだとしている。しかし、2.6.21 以前のカーネルの
Linux ではエラーにならず、対応する秒数の分だけそのタイマーの秒の値が暗
黙に調整される。カーネル 2.6.22 以降では、この標準非準拠の動作は修正 さ
れ、 tv_usec の値が不適切な場合には EINVAL エラーとなる。
関連項目
gettimeofday(2), sigaction(2), signal(2), timer_create(2), timerfd_cre-
ate(2), time(7)
Linux 2009-03-15 GETITIMER(2)
GETITIMER(2) Linux Programmer’s Manual GETITIMER(2)
NAME
getitimer, setitimer - get or set value of an interval timer
SYNOPSIS
#include
int getitimer(int which, struct itimerval *curr_value);
int setitimer(int which, const struct itimerval *new_value,
struct itimerval *old_value);
DESCRIPTION
The system provides each process with three interval timers, each
decrementing in a distinct time domain. When any timer expires, a sig-
nal is sent to the process, and the timer (potentially) restarts.
ITIMER_REAL decrements in real time, and delivers SIGALRM upon expi-
ration.
ITIMER_VIRTUAL decrements only when the process is executing, and
delivers SIGVTALRM upon expiration.
ITIMER_PROF decrements both when the process executes and when the
system is executing on behalf of the process. Coupled
with ITIMER_VIRTUAL, this timer is usually used to pro-
file the time spent by the application in user and ker-
nel space. SIGPROF is delivered upon expiration.
Timer values are defined by the following structures:
struct itimerval {
struct timeval it_interval; /* next value */
struct timeval it_value; /* current value */
};
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
The function getitimer() fills the structure pointed to by curr_value
with the current setting for the timer specified by which (one of
ITIMER_REAL, ITIMER_VIRTUAL, or ITIMER_PROF). The element it_value is
set to the amount of time remaining on the timer, or zero if the timer
is disabled. Similarly, it_interval is set to the reset value.
The function setitimer() sets the specified timer to the value in
new_value. If old_value is non-NULL, the old value of the timer is
stored there.
Timers decrement from it_value to zero, generate a signal, and reset to
it_interval. A timer which is set to zero (it_value is zero or the
timer expires and it_interval is zero) stops.
Both tv_sec and tv_usec are significant in determining the duration of
a timer.
Timers will never expire before the requested time, but may expire some
(short) time afterwards, which depends on the system timer resolution
and on the system load; see time(7). (But see BUGS below.) Upon expi-
ration, a signal will be generated and the timer reset. If the timer
expires while the process is active (always true for ITIMER_VIRTUAL)
the signal will be delivered immediately when generated. Otherwise the
delivery will be offset by a small time dependent on the system load-
ing.
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
ERRORS
EFAULT new_value, old_value, or curr_value is not valid a pointer.
EINVAL which is not one of ITIMER_REAL, ITIMER_VIRTUAL, or ITIMER_PROF;
or (since Linux 2.6.22) one of the tv_usec fields in the struc-
ture pointed to by new_value contains a value outside the range
0 to 999999.
CONFORMING TO
POSIX.1-2001, SVr4, 4.4BSD (this call first appeared in 4.2BSD).
POSIX.1-2008 marks getitimer() and setitimer() obsolete, recommending
the use of the POSIX timers API (timer_gettime(2), timer_settime(2),
etc.) instead.
NOTES
A child created via fork(2) does not inherit its parent’s interval
timers. Interval timers are preserved across an execve(2).
POSIX.1 leaves the interaction between setitimer() and the three inter-
faces alarm(2), sleep(3), and usleep(3) unspecified.
BUGS
The generation and delivery of a signal are distinct, and only one
instance of each of the signals listed above may be pending for a pro-
cess. Under very heavy loading, an ITIMER_REAL timer may expire before
the signal from a previous expiration has been delivered. The second
signal in such an event will be lost.
On Linux kernels before 2.6.16, timer values are represented in
jiffies. If a request is made set a timer with a value whose jiffies
representation exceeds MAX_SEC_IN_JIFFIES (defined in
include/linux/jiffies.h), then the timer is silently truncated to this
ceiling value. On Linux/i386 (where, since Linux 2.6.13, the default
jiffy is 0.004 seconds), this means that the ceiling value for a timer
is approximately 99.42 days. Since Linux 2.6.16, the kernel uses a
different internal representation for times, and this ceiling is
removed.
On certain systems (including i386), Linux kernels before version
2.6.12 have a bug which will produce premature timer expirations of up
to one jiffy under some circumstances. This bug is fixed in kernel
2.6.12.
POSIX.1-2001 says that setitimer() should fail if a tv_usec value is
specified that is outside of the range 0 to 999999. However, in ker-
nels up to and including 2.6.21, Linux does not give an error, but
instead silently adjusts the corresponding seconds value for the timer.
From kernel 2.6.22 onwards, this non-conformance has been repaired: an
improper tv_usec value results in an EINVAL error.
SEE ALSO
gettimeofday(2), sigaction(2), signal(2), timer_create(2), timerfd_cre-
ate(2), time(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2009-03-15 GETITIMER(2)