1CLOCK_NANOSLEEP(2) Linux Programmer's Manual CLOCK_NANOSLEEP(2)
2
3
4
6 clock_nanosleep - high-resolution sleep with specifiable clock
7
9 #include <time.h>
10
11 int clock_nanosleep(clockid_t clock_id, int flags,
12 const struct timespec *request,
13 struct timespec *remain);
14
15 Link with -lrt.
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 clock_nanosleep(): _XOPEN_SOURCE >= 600
20
22 Like nanosleep(2), clock_nanosleep() allows the caller to sleep for an
23 interval specified with nanosecond precision. It differs in allowing
24 the caller to select the clock against which the sleep interval is to
25 be measured, and in allowing the sleep interval to be specified as
26 either an absolute or a relative value.
27
28 The time values passed to and returned by this call are specified using
29 timespec structures, defined as follows:
30
31 struct timespec {
32 time_t tv_sec; /* seconds */
33 long tv_nsec; /* nanoseconds [0 .. 999999999] */
34 };
35
36 The clock_id argument specifies the clock against which the sleep
37 interval is to be measured. This argument can have one of the follow‐
38 ing values:
39
40 CLOCK_REALTIME A settable system-wide real-time clock.
41
42 CLOCK_MONOTONIC A non-settable, monotonically increasing clock that
43 measures time since some unspecified point in the past
44 that does not change after system startup.
45
46 CLOCK_PROCESS_CPUTIME_ID
47 A settable per-process clock that measures CPU time
48 consumed by all threads in the process.
49
50 See clock_getres(2) for further details on these clocks.
51
52 If flags is 0, then the value specified in request is interpreted as an
53 interval relative to the current value of the clock specified by
54 clock_id.
55
56 If flags is TIMER_ABSTIME, then request is interpreted as an absolute
57 time as measured by the clock, clock_id. If request is less than or
58 equal to the current value of the clock, then clock_nanosleep() returns
59 immediately without suspending the calling thread.
60
61 clock_nanosleep() suspends the execution of the calling thread until
62 either at least the time specified by request has elapsed, or a signal
63 is delivered that causes a signal handler to be called or that termi‐
64 nates the process.
65
66 If the call is interrupted by a signal handler, clock_nanosleep()
67 returns -1, and sets errno to EINTR. In addition, if remain is not
68 NULL, and flags was not TIMER_ABSTIME, it returns the remaining unslept
69 time in remain. This value can then be used to call clock_nanosleep()
70 again and complete a (relative) sleep.
71
73 On successfully sleeping for the requested interval, clock_nanosleep()
74 returns 0. If the call is interrupted by a signal handler or encoun‐
75 ters an error, then it returns a positive error number.
76
78 EFAULT request or remain specified an invalid address.
79
80 EINTR The sleep was interrupted by a signal handler.
81
82 EINVAL The value in the tv_nsec field was not in the range 0 to
83 999999999 or tv_sec was negative.
84
85 EINVAL clock_id was invalid. (CLOCK_THREAD_CPUTIME_ID is not a permit‐
86 ted value for clock_id.)
87
89 The clock_nanosleep() system call first appeared in Linux 2.6. Support
90 is available in glibc since version 2.1.
91
93 POSIX.1-2001.
94
96 If the interval specified in request is not an exact multiple of the
97 granularity underlying clock (see time(7)), then the interval will be
98 rounded up to the next multiple. Furthermore, after the sleep com‐
99 pletes, there may still be a delay before the CPU becomes free to once
100 again execute the calling thread.
101
102 Using an absolute timer is useful for preventing timer drift problems
103 of the type described in nanosleep(2). (Such problems are exacerbated
104 in programs that try to restart a relative sleep that is repeatedly
105 interrupted by signals.) To perform a relative sleep that avoids these
106 problems, call clock_gettime(2) for the desired clock, add the desired
107 interval to the returned time value, and then call clock_nanosleep()
108 with the TIMER_ABSTIME flag.
109
110 clock_nanosleep() is never restarted after being interrupted by a sig‐
111 nal handler, regardless of the use of the sigaction(2) SA_SIGACTION
112 flag.
113
114 The remain argument is unused, and unnecessary, when flags is
115 TIMER_ABSTIME. (An absolute sleep can be restarted using the same
116 request argument.)
117
118 POSIX.1 specifies that clock_nanosleep() has no effect on signals dis‐
119 positions or the signal mask.
120
121 POSIX.1 specifies that after changing the value of the CLOCK_REALTIME
122 clock via clock_settime(2), the new clock value shall be used to deter‐
123 mine the time at which a thread blocked on an absolute
124 clock_nanosleep() will wake up; if the new clock value falls past the
125 end of the sleep interval, then the clock_nanosleep() call will return
126 immediately.
127
128 POSIX.1 specifies that changing the value of the CLOCK_REALTIME clock
129 via clock_settime(2) shall have no effect on a thread that is blocked
130 on a relative clock_nanosleep().
131
133 nanosleep(2), timer_create(2), clock_getres(2), sleep(3), usleep(3),
134 time(7)
135
137 This page is part of release 3.22 of the Linux man-pages project. A
138 description of the project, and information about reporting bugs, can
139 be found at http://www.kernel.org/doc/man-pages/.
140
141
142
143Linux 2008-07-09 CLOCK_NANOSLEEP(2)