1CLOCK_NANOSLEEP(P)         POSIX Programmer's Manual        CLOCK_NANOSLEEP(P)
2
3
4

NAME

6       clock_nanosleep   -   high  resolution  sleep  with  specifiable  clock
7       (ADVANCED REALTIME)
8

SYNOPSIS

10       #include <time.h>
11
12       int clock_nanosleep(clockid_t clock_id, int flags,
13              const struct timespec *rqtp, struct timespec *rmtp);
14
15

DESCRIPTION

17       If the flag TIMER_ABSTIME  is  not  set  in  the  flags  argument,  the
18       clock_nanosleep()  function  shall  cause the current thread to be sus‐
19       pended from execution until either the time interval specified  by  the
20       rqtp  argument  has  elapsed,  or  a signal is delivered to the calling
21       thread and its action is to invoke a signal-catching function,  or  the
22       process  is terminated. The clock used to measure the time shall be the
23       clock specified by clock_id.
24
25       If  the  flag  TIMER_ABSTIME  is  set  in  the  flags   argument,   the
26       clock_nanosleep()  function  shall  cause the current thread to be sus‐
27       pended from execution until either the time value of the  clock  speci‐
28       fied  by clock_id reaches the absolute time specified by the rqtp argu‐
29       ment, or a signal is delivered to the calling thread and its action  is
30       to invoke a signal-catching function, or the process is terminated. If,
31       at the time of the call, the time value specified by rqtp is less  than
32       or   equal   to   the   time   value   of  the  specified  clock,  then
33       clock_nanosleep() shall return  immediately  and  the  calling  process
34       shall not be suspended.
35
36       The  suspension  time  caused  by  this  function  may  be  longer than
37       requested because the argument value is rounded up to an integer multi‐
38       ple  of  the  sleep  resolution,  or because of the scheduling of other
39       activity by the system. But, except for the case of  being  interrupted
40       by  a  signal,  the  suspension time for the relative clock_nanosleep()
41       function (that is, with the TIMER_ABSTIME flag not set)  shall  not  be
42       less  than the time interval specified by rqtp, as measured by the cor‐
43       responding clock. The suspension  for  the  absolute  clock_nanosleep()
44       function  (that is, with the TIMER_ABSTIME flag set) shall be in effect
45       at least until the value of the corresponding clock reaches  the  abso‐
46       lute  time  specified by rqtp, except for the case of being interrupted
47       by a signal.
48
49       The use of the clock_nanosleep() function shall have no effect  on  the
50       action or blockage of any signal.
51
52       The  clock_nanosleep()  function  shall  fail  if the clock_id argument
53       refers to the CPU-time clock of the calling thread. It  is  unspecified
54       whether clock_id values of other CPU-time clocks are allowed.
55

RETURN VALUE

57       If  the  clock_nanosleep()  function returns because the requested time
58       has elapsed, its return value shall be zero.
59
60       If the clock_nanosleep() function returns because it  has  been  inter‐
61       rupted  by a signal, it shall return the corresponding error value. For
62       the relative clock_nanosleep() function, if the rmtp argument  is  non-
63       NULL,  the timespec structure referenced by it shall be updated to con‐
64       tain the amount of time remaining in the interval (the  requested  time
65       minus  the  time  actually  slept).  If  the rmtp argument is NULL, the
66       remaining time is not returned. The absolute clock_nanosleep() function
67       has no effect on the structure referenced by rmtp.
68
69       If  clock_nanosleep()  fails,  it  shall return the corresponding error
70       value.
71

ERRORS

73       The clock_nanosleep() function shall fail if:
74
75       EINTR  The clock_nanosleep() function was interrupted by a signal.
76
77       EINVAL The rqtp argument specified a nanosecond value less than zero or
78              greater than or equal to 1000 million; or the TIMER_ABSTIME flag
79              was specified in flags and the  rqtp  argument  is  outside  the
80              range for the clock specified by clock_id; or the clock_id argu‐
81              ment does not specify a known clock, or specifies  the  CPU-time
82              clock of the calling thread.
83
84       ENOTSUP
85              The    clock_id   argument   specifies   a   clock   for   which
86              clock_nanosleep() is not supported, such as a CPU-time clock.
87
88
89       The following sections are informative.
90

EXAMPLES

92       None.
93

APPLICATION USAGE

95       Calling clock_nanosleep() with the value TIMER_ABSTIME not set  in  the
96       flags  argument  and with a clock_id of CLOCK_REALTIME is equivalent to
97       calling nanosleep() with the same rqtp and rmtp arguments.
98

RATIONALE

100       The  nanosleep()  function  specifies  that   the   system-wide   clock
101       CLOCK_REALTIME  is  used to measure the elapsed time for this time ser‐
102       vice. However, with the introduction of the monotonic clock CLOCK_MONO‐
103       TONIC  a  new relative sleep function is needed to allow an application
104       to take advantage of the special characteristics of this clock.
105
106       There are many applications in which a process needs  to  be  suspended
107       and  then  activated  multiple times in a periodic way; for example, to
108       poll the status of a non-interrupting device or to  refresh  a  display
109       device.  For  these cases, it is known that precise periodic activation
110       cannot be achieved with a  relative  sleep()  or  nanosleep()  function
111       call.  Suppose,  for  example,  a periodic process that is activated at
112       time T0, executes for a while, and then wants to suspend  itself  until
113       time  T0+  T,  the  period  being  T.  If this process wants to use the
114       nanosleep() function, it must first call  clock_gettime()  to  get  the
115       current  time,  then  calculate the difference between the current time
116       and T0+ T and, finally, call nanosleep() using the  computed  interval.
117       However,  the process could be preempted by a different process between
118       the two function calls, and in this case the interval computed would be
119       wrong; the process would wake up later than desired. This problem would
120       not occur with the absolute clock_nanosleep() function, since only  one
121       function  call  would  be  necessary  to  suspend the process until the
122       desired time.  In other cases, however, a relative sleep is needed, and
123       that is why both functionalities are required.
124
125       Although  it  is  possible  to  implement  periodic processes using the
126       timers interface, this implementation would require the use of signals,
127       and the reservation of some signal numbers. In this regard, the reasons
128       for including an absolute version of the clock_nanosleep() function  in
129       IEEE Std 1003.1-2001  are the same as for the inclusion of the relative
130       nanosleep().
131
132       It is also possible  to  implement  precise  periodic  processes  using
133       pthread_cond_timedwait(),  in  which  an  absolute timeout is specified
134       that takes effect if the condition variable involved is never signaled.
135       However,  the use of this interface is unnatural, and involves perform‐
136       ing other operations on mutexes and condition variables that  imply  an
137       unnecessary  overhead.  Furthermore,  pthread_cond_timedwait()  is  not
138       available in implementations that do not support threads.
139
140       Although the interface of the relative and absolute versions of the new
141       high  resolution  sleep service is the same clock_nanosleep() function,
142       the rmtp argument is only used in the relative sleep. This argument  is
143       needed  in the relative clock_nanosleep() function to reissue the func‐
144       tion call if it is interrupted by a signal, but it is not needed in the
145       absolute clock_nanosleep() function call; if the call is interrupted by
146       a signal, the absolute clock_nanosleep() function can be invoked  again
147       with the same rqtp argument used in the interrupted call.
148

FUTURE DIRECTIONS

150       None.
151

SEE ALSO

153       clock_getres() , nanosleep() , pthread_cond_timedwait() , sleep() , the
154       Base Definitions volume of IEEE Std 1003.1-2001, <time.h>
155
157       Portions of this text are reprinted and reproduced in  electronic  form
158       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
159       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
160       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
161       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
162       event of any discrepancy between this version and the original IEEE and
163       The Open Group Standard, the original IEEE and The Open Group  Standard
164       is  the  referee document. The original Standard can be obtained online
165       at http://www.opengroup.org/unix/online.html .
166
167
168
169IEEE/The Open Group                  2003                   CLOCK_NANOSLEEP(P)
Impressum