- 论坛徽章:
- 20
|
本帖最后由 nswcfd 于 2016-03-30 11:02 编辑
仅供参考。
两台机器上man nanosleep的区别。
版本1,Linux 2.6.9 @ 2004-10-24,注意BUGS部分。
NAME
nanosleep - pause execution for a specified time
SYNOPSIS
#define _POSIX_C_SOURCE 199309 #include <time.h>
int nanosleep(const struct timespec *req, struct timespec *rem);
BUGS
The current implementation of nanosleep() is based on the normal kernel timer mechanism, which has a resolution of 1/HZ s (see time(7)). Therefore, nanosleep() pauses always for at
least the specified time, however it can take up to 10 ms longer than specified until the process becomes runnable again. For the same reason, the value returned in case of a deliv-
ered signal in *rem is usually rounded to the next larger multiple of 1/HZ s.
Old behaviour
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 precision when called from a process 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.
In Linux 2.4, if nanosleep() is stopped by a signal (e.g., SIGTSTP), then the call fails with the error EINTR after the process is resumed by a SIGCONT signal. If the system call is
subsequently restarted, then the time that the process spent in the stopped state is not counted against the sleep interval.
CONFORMING TO
POSIX.1-2001.
SEE ALSO
sched_setscheduler(2), timer_create(2), sleep(3), usleep(3)
Linux 2.6.9 2004-10-24 NANOSLEEP(2)
版本2,2009-01-19,3.22,没有前面的BUG描述(受限于timer精度)。
NAME
nanosleep - high-resolution sleep
SYNOPSIS
#include <time.h>
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
CONFORMING TO
POSIX.1-2001.
NOTES
If the interval specified in req is not an exact multiple of the granularity 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 problematic 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 absolute 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_settime() 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, independently 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
precision 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) |
|