1CLOCK_NANOSLEEP(3P) POSIX Programmer's Manual CLOCK_NANOSLEEP(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 clock_nanosleep - high resolution sleep with specifiable clock
13 (ADVANCED REALTIME)
14
16 #include <time.h>
17
18 int clock_nanosleep(clockid_t clock_id, int flags,
19 const struct timespec *rqtp, struct timespec *rmtp);
20
21
23 If the flag TIMER_ABSTIME is not set in the flags argument, the
24 clock_nanosleep() function shall cause the current thread to be sus‐
25 pended from execution until either the time interval specified by the
26 rqtp argument has elapsed, or a signal is delivered to the calling
27 thread and its action is to invoke a signal-catching function, or the
28 process is terminated. The clock used to measure the time shall be the
29 clock specified by clock_id.
30
31 If the flag TIMER_ABSTIME is set in the flags argument, the
32 clock_nanosleep() function shall cause the current thread to be sus‐
33 pended from execution until either the time value of the clock speci‐
34 fied by clock_id reaches the absolute time specified by the rqtp argu‐
35 ment, or a signal is delivered to the calling thread and its action is
36 to invoke a signal-catching function, or the process is terminated. If,
37 at the time of the call, the time value specified by rqtp is less than
38 or equal to the time value of the specified clock, then
39 clock_nanosleep() shall return immediately and the calling process
40 shall not be suspended.
41
42 The suspension time caused by this function may be longer than
43 requested because the argument value is rounded up to an integer multi‐
44 ple of the sleep resolution, or because of the scheduling of other
45 activity by the system. But, except for the case of being interrupted
46 by a signal, the suspension time for the relative clock_nanosleep()
47 function (that is, with the TIMER_ABSTIME flag not set) shall not be
48 less than the time interval specified by rqtp, as measured by the cor‐
49 responding clock. The suspension for the absolute clock_nanosleep()
50 function (that is, with the TIMER_ABSTIME flag set) shall be in effect
51 at least until the value of the corresponding clock reaches the abso‐
52 lute time specified by rqtp, except for the case of being interrupted
53 by a signal.
54
55 The use of the clock_nanosleep() function shall have no effect on the
56 action or blockage of any signal.
57
58 The clock_nanosleep() function shall fail if the clock_id argument
59 refers to the CPU-time clock of the calling thread. It is unspecified
60 whether clock_id values of other CPU-time clocks are allowed.
61
63 If the clock_nanosleep() function returns because the requested time
64 has elapsed, its return value shall be zero.
65
66 If the clock_nanosleep() function returns because it has been inter‐
67 rupted by a signal, it shall return the corresponding error value. For
68 the relative clock_nanosleep() function, if the rmtp argument is non-
69 NULL, the timespec structure referenced by it shall be updated to con‐
70 tain the amount of time remaining in the interval (the requested time
71 minus the time actually slept). If the rmtp argument is NULL, the
72 remaining time is not returned. The absolute clock_nanosleep() function
73 has no effect on the structure referenced by rmtp.
74
75 If clock_nanosleep() fails, it shall return the corresponding error
76 value.
77
79 The clock_nanosleep() function shall fail if:
80
81 EINTR The clock_nanosleep() function was interrupted by a signal.
82
83 EINVAL The rqtp argument specified a nanosecond value less than zero or
84 greater than or equal to 1000 million; or the TIMER_ABSTIME flag
85 was specified in flags and the rqtp argument is outside the
86 range for the clock specified by clock_id; or the clock_id argu‐
87 ment does not specify a known clock, or specifies the CPU-time
88 clock of the calling thread.
89
90 ENOTSUP
91 The clock_id argument specifies a clock for which
92 clock_nanosleep() is not supported, such as a CPU-time clock.
93
94
95 The following sections are informative.
96
98 None.
99
101 Calling clock_nanosleep() with the value TIMER_ABSTIME not set in the
102 flags argument and with a clock_id of CLOCK_REALTIME is equivalent to
103 calling nanosleep() with the same rqtp and rmtp arguments.
104
106 The nanosleep() function specifies that the system-wide clock
107 CLOCK_REALTIME is used to measure the elapsed time for this time ser‐
108 vice. However, with the introduction of the monotonic clock CLOCK_MONO‐
109 TONIC a new relative sleep function is needed to allow an application
110 to take advantage of the special characteristics of this clock.
111
112 There are many applications in which a process needs to be suspended
113 and then activated multiple times in a periodic way; for example, to
114 poll the status of a non-interrupting device or to refresh a display
115 device. For these cases, it is known that precise periodic activation
116 cannot be achieved with a relative sleep() or nanosleep() function
117 call. Suppose, for example, a periodic process that is activated at
118 time T0, executes for a while, and then wants to suspend itself until
119 time T0+ T, the period being T. If this process wants to use the
120 nanosleep() function, it must first call clock_gettime() to get the
121 current time, then calculate the difference between the current time
122 and T0+ T and, finally, call nanosleep() using the computed interval.
123 However, the process could be preempted by a different process between
124 the two function calls, and in this case the interval computed would be
125 wrong; the process would wake up later than desired. This problem would
126 not occur with the absolute clock_nanosleep() function, since only one
127 function call would be necessary to suspend the process until the
128 desired time. In other cases, however, a relative sleep is needed, and
129 that is why both functionalities are required.
130
131 Although it is possible to implement periodic processes using the
132 timers interface, this implementation would require the use of signals,
133 and the reservation of some signal numbers. In this regard, the reasons
134 for including an absolute version of the clock_nanosleep() function in
135 IEEE Std 1003.1-2001 are the same as for the inclusion of the relative
136 nanosleep().
137
138 It is also possible to implement precise periodic processes using
139 pthread_cond_timedwait(), in which an absolute timeout is specified
140 that takes effect if the condition variable involved is never signaled.
141 However, the use of this interface is unnatural, and involves perform‐
142 ing other operations on mutexes and condition variables that imply an
143 unnecessary overhead. Furthermore, pthread_cond_timedwait() is not
144 available in implementations that do not support threads.
145
146 Although the interface of the relative and absolute versions of the new
147 high resolution sleep service is the same clock_nanosleep() function,
148 the rmtp argument is only used in the relative sleep. This argument is
149 needed in the relative clock_nanosleep() function to reissue the func‐
150 tion call if it is interrupted by a signal, but it is not needed in the
151 absolute clock_nanosleep() function call; if the call is interrupted by
152 a signal, the absolute clock_nanosleep() function can be invoked again
153 with the same rqtp argument used in the interrupted call.
154
156 None.
157
159 clock_getres(), nanosleep(), pthread_cond_timedwait(), sleep(), the
160 Base Definitions volume of IEEE Std 1003.1-2001, <time.h>
161
163 Portions of this text are reprinted and reproduced in electronic form
164 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
165 -- Portable Operating System Interface (POSIX), The Open Group Base
166 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
167 Electrical and Electronics Engineers, Inc and The Open Group. In the
168 event of any discrepancy between this version and the original IEEE and
169 The Open Group Standard, the original IEEE and The Open Group Standard
170 is the referee document. The original Standard can be obtained online
171 at http://www.opengroup.org/unix/online.html .
172
173
174
175IEEE/The Open Group 2003 CLOCK_NANOSLEEP(3P)