nanosleepのヘルプ・マニュアル
日本語 英語
nanosleep --help
man nanosleep
NANOSLEEP(2) Linux Programmer’s Manual NANOSLEEP(2)
名前
nanosleep - 高精度なスリープ
書式
#include
int nanosleep(const struct timespec *req, struct timespec *rem);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
nanosleep(): _POSIX_C_SOURCE >= 199309L
説明
nanosleep() は、少なくとも *req で指定された時間の間、プログラムの実行
を遅延させる。 nanosleep() は、呼び出したスレッドの実行を、少なく と も
*req で指定された時間の間、もしくは呼び出したスレッドでハンドラの起動の
きっかけとなるシグナル、またはプロセスを終了させるシグナルの配送が行 わ
れるまで一時停止する。
呼び出しがシグナルハンドラにより割り込まれた場合、 nanosleep は -1 を返
し、 errno に EINTR を設定し、 rem が NULL でなければ残りの時間 を rem
が指す構造体に格納する。 *rem の値を使うと、 nanosleep() をもう一度呼び
出して、指定した時間の停止を完了させることができる (但し、「注意」の 節
を参照のこと)。
ナ ノ秒刻みの時間間隔を指定するのに timespec 構造体が使用される。この構
造体は次のように定義されている。
time_t tv_sec; /* 秒 */
long tv_nsec; /* ナノ秒 */
};
ナノ秒のフィールドの値は 0 から 999999999 の範囲になければならない。
sleep(3) や usleep(3) に比べると nanosleep() には以下のような利点 が あ
る: 停止期間の指定に関して高い時間分解能が提供されている。シグナルと互
いに影響を及ぼすことがないと POSIX.1 で明示的に規定されている。シグナル
ハンドラによって割り込まれた際に、停止を再開するのがより簡単にできる。
返り値
要 求された期間の停止に成功した場合、 nanosleep() は 0 を返す。呼び出し
がシグナルハンドラにより割り込まれたり、エラーが発生した場合は、-1 を返
し、 errno にエラー内容を示す値を設定する。
エラー
EFAULT ユーザ空間からの情報のコピーで問題があった。
EINTR 禁止 (block) されていないシグナルがスレッドに配送されて、停止が
中断された。スレッドが簡単に nanosleep() を再び呼び出して停止 を
続けることができるように、残りの停止時間が *rem に格納される。
EINVAL tv_nsec フィールドの値が 0 から 999999999 までの範囲になかったか
、 tv_sec が負であった。
準拠
POSIX.1-2001.
注意
req で指定された期間が、内部で使用されるクロックの粒度の倍数になって い
な い 場 合、期間は一番近い倍数に切り上げられる。また、停止が完了した後
、CPU が呼び出し元のスレッドを再び実行できるようになるまでには遅延が 入
る。
シグナルによる割り込み後に繰り返し再開された場合、 nanosleep() の停止が
相対的な期間であることは問題となることがある。これは、呼び出しの割り 込
み から再開までの間の時間が原因で停止が最終的に完了した際に時間にずれが
発生するからである。この問題は、絶対時刻が指定できる clock_nanosleep(2)
を使うことで回避できる。
POSIX.1 は、 nanosleep() は CLOCK_REALTIME に対して時刻を計測するべきだ
と規定している。しかしながら、Linux は CLOCK_MONOTONIC クロックを用いて
時 刻 を 計測している。このことはおそらく問題にならないだろう。なぜなら
、POSIX.1 の clock_settime() の仕様には、 CLOCK_REALTIME の不連続な変化
は nanosleep() に影響すべきではない、と書かれているからである。
clock_settime() 経由で CLOCK_REALTIME クロックの値を設定しても、
nanosleep() 関数などの CLOCK_REALTIME に基づくサービスにより相対
的な期間だけ実行を停止するスレッドには影響はない。結果として、ク
ロック値が更新前か後かに関わらず、要求された相対的な時間が経過す
ると満了することになる。
以前の動作
(例えば、時間が重要な意味を持つハードウェアを制御する場合など) より正確
な停止を必要とするアプリケーションに対応するために、 nanosleep() は、マ
イ クロ秒精度のビジー・ウェイトを利用することで、 2 ms 以下の停止を行う
ことができた。但し、この機能を利用するには、呼び出し元の ス レ ッ ド が
SCHED_FIFO や SCHED_RR といったリアルタイム・ポリシーの元でスケジューリ
ングされている必要があった。この特別な拡張はカーネル 2.5.39 で削除さ れ
た。したがって、現在の 2.4 系列のカーネルにはこの機能が存在するが、 2.6
系列のカーネルにはない。
バグ
Linux 2.4 では、 nanosleep() が (SIGTSTP などの) シグナルにより停止され
た 場合、 nanosleep() の呼び出しは SIGCONT シグナルによるスレッドの再開
後に EINTR エラーで失敗する。システムコールがこの後で再スタートされた場
合 、スレッドが停止状態にある間に経過した時間は停止期間としてカウント「
されない」。
関連項目
clock_nanosleep(2), sched_setscheduler(2), sleep(3), timer_create(2),
usleep(3), time(7)
Linux 2009-01-19 NANOSLEEP(2)
NANOSLEEP(2) Linux Programmer’s Manual NANOSLEEP(2)
NAME
nanosleep - high-resolution sleep
SYNOPSIS
#include
int nanosleep(const struct timespec *req, struct timespec *rem);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
nanosleep(): _POSIX_C_SOURCE >= 199309L
DESCRIPTION
nanosleep() suspends the execution of the calling thread until either
at least the time specified in *req has elapsed, or the delivery of a
signal that triggers the invocation of a handler in the calling thread
or that terminates the process.
If the call is interrupted by a signal handler, nanosleep() returns -1,
sets errno to EINTR, and writes the remaining time into the structure
pointed to by rem unless rem is NULL. The value of *rem can then be
used to call nanosleep() again and complete the specified pause (but
see NOTES).
The structure timespec is used to specify intervals of time with
nanosecond precision. It is defined as follows:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
The value of the nanoseconds field must be in the range 0 to 999999999.
Compared to sleep(3) and usleep(3), nanosleep() has the following
advantages: it provides a higher resolution for specifying the sleep
interval; POSIX.1 explicitly specifies that it does not interact with
signals; and it makes the task of resuming a sleep that has been inter-
rupted by a signal handler easier.
RETURN VALUE
On successfully sleeping for the requested interval, nanosleep()
returns 0. If the call is interrupted by a signal handler or encoun-
ters an error, then it returns -1, with errno set to indicate the
error.
ERRORS
EFAULT Problem with copying information from user space.
EINTR The pause has been interrupted by a non-blocked signal that was
delivered to the thread. The remaining sleep time has been
written into *rem so that the thread can easily call nanosleep()
again and continue with the pause.
EINVAL The value in the tv_nsec field was not in the range 0 to
999999999 or tv_sec was negative.
CONFORMING TO
POSIX.1-2001.
NOTES
If the interval specified in req is not an exact multiple of the granu-
larity underlying clock (see time(7)), then the interval will be
rounded up to the next multiple. Furthermore, after the sleep com-
pletes, there may still be a delay before the CPU becomes free to once
again execute the calling thread.
The fact that nanosleep() sleeps for a relative interval can be prob-
lematic if the call is repeatedly restarted after being interrupted by
signals, since the time between the interruptions and restarts of the
call will lead to drift in the time when the sleep finally completes.
This problem can be avoided by using clock_nanosleep(2) with an abso-
lute time value.
POSIX.1 specifies that nanosleep() should measure time against the
CLOCK_REALTIME clock. However, Linux measures the time using the
CLOCK_MONOTONIC clock. This probably does not matter, since the
POSIX.1 specification for clock_settime() says that discontinuous
changes in CLOCK_REALTIME should not affect nanosleep():
Setting the value of the CLOCK_REALTIME clock via clock_set-
time() shall have no effect on threads that are blocked waiting
for a relative time service based upon this clock, including the
nanosleep() function; ... Consequently, these time services
shall expire when the requested relative interval elapses, inde-
pendently of the new or old value of the clock.
Old behavior
In order to support applications requiring much more precise pauses
(e.g., in order to control some time-critical hardware), nanosleep()
would handle pauses of up to 2 ms by busy waiting with microsecond pre-
cision when called from a thread scheduled under a real-time policy
like SCHED_FIFO or SCHED_RR. This special extension was removed in
kernel 2.5.39, hence is still present in current 2.4 kernels, but not
in 2.6 kernels.
BUGS
In Linux 2.4, if nanosleep() is stopped by a signal (e.g., SIGTSTP),
then the call fails with the error EINTR after the thread is resumed by
a SIGCONT signal. If the system call is subsequently restarted, then
the time that the thread spent in the stopped state is not counted
against the sleep interval.
SEE ALSO
clock_nanosleep(2), sched_setscheduler(2), sleep(3), timer_create(2),
usleep(3), 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-01-19 NANOSLEEP(2)