1.\" $NetBSD: nanosleep.2,v 1.25 2017/10/25 17:58:04 abhinav Exp $ 2.\" 3.\" Copyright (c) 1986, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)sleep.3 8.1 (Berkeley) 6/4/93 31.\" 32.Dd November 11, 2016 33.Dt NANOSLEEP 2 34.Os 35.Sh NAME 36.Nm nanosleep , 37.Nm clock_nanosleep 38.Nd high resolution sleep 39.Sh LIBRARY 40.Lb libc 41.Sh SYNOPSIS 42.In time.h 43.Ft int 44.Fn clock_nanosleep "clockid_t clock_id" "int flags" "const struct timespec *rqtp" "struct timespec *rmtp" 45.Ft int 46.Fn nanosleep "const struct timespec *rqtp" "struct timespec *rmtp" 47.Sh DESCRIPTION 48If the 49.Dv TIMER_ABSTIME 50flag is not set in the 51.Fa flags 52argument, then 53.Fn clock_nanosleep 54suspends execution of the calling thread until either the number of 55seconds and nanoseconds specified in the 56.Fa rqtp 57argument have elapsed using the clock in the 58.Fa clock_id 59argument, or a signal is delivered to the calling process and its 60action is to invoke a signal catching function or to terminate the 61process. 62.Pp 63If the 64.Dv TIMER_ABSTIME 65flag is set in the 66.Fa flags 67argument, then 68.Fn clock_nanosleep 69suspends execution of the calling thread until either the value 70of the clock specified in the 71.Fa clock_id 72argument reaches the value of the 73.Fa rqtp 74argument in seconds and nanoseconds, 75or a signal is delivered to the calling process and its 76action is to invoke a signal catching function or to terminate the 77process. 78.Pp 79The suspension time may be longer than requested due to the 80scheduling of other activity by the system. 81.Pp 82The 83.Fn nanosleep 84function behaves like 85.Fn clock_nanosleep 86with the 87.Fa clock_id 88argument equal to 89.Dv CLOCK_MONOTONIC 90and the 91.Fa flags 92argument having 93.Dv TIMER_ABSTIME 94not set. 95.Pp 96The 97.Em struct timespec 98is described in 99.Xr timespec 3 . 100The 101.Fa clock_id 102specified is the time source, which is described in 103.Xr clock_gettime 2 . 104.Sh RETURN VALUES 105If the 106.Fn clock_nanosleep 107or the 108.Fn nanosleep 109functions return because the requested time has elapsed, the value 110returned will be zero. 111.Pp 112If the 113.Fn clock_nanosleep 114or the 115.Fn nanosleep 116functions return due to the delivery of a signal, then 117.Fn clock_nanosleep 118will return directly the error number, and 119.Fn nanosleep 120will return \-1, and the global variable 121.Va errno 122will be set to indicate the interruption. 123If 124.Fa rmtp 125is 126.Pf non- Dv NULL , 127the timespec structure it references is updated to contain the 128unslept amount (the request time minus the time actually slept), 129unless it is called from 130.Fn clock_nanosleep 131with a 132.Fa flags 133argument of 134.Dv TIMER_ABSTIME . 135In that case, the 136.Fa rmtp 137argument is left unmodified. 138.Sh ERRORS 139If any of the following conditions occur, the 140.Fn nanosleep 141function shall return \-1 and set 142.Va errno 143to the corresponding value, and the 144.Fn clock_nanosleep 145function shall return the error number directly. 146.Bl -tag -width Er 147.It Bq Er EFAULT 148Either 149.Fa rqtp 150or 151.Fa rmtp 152points to memory that is not a valid part of the process 153address space. 154.It Bq Er EINTR 155.Nm 156was interrupted by the delivery of a signal. 157.It Bq Er EINVAL 158.Fa rqtp 159specified a nanosecond value less than zero or greater than 1000 million. 160.It Bq Er ENOSYS 161.Nm 162is not supported by this implementation. 163.It Bq Er ENOTSUP 164for 165.Fn clock_nanosleep , 166the clock specified in the 167.Fa clock_id 168argument is not supported. 169.El 170.Sh SEE ALSO 171.Xr clock_gettime 2 , 172.Xr sleep 3 , 173.Xr timespec 3 174.Sh STANDARDS 175The 176.Fn nanosleep 177function conforms to 178.St -p1003.1b-93 . 179The 180.Fn clock_nanosleep 181function conforms to 182.St -p1003.1-2008 . 183.Sh BUGS 184The 185.Nx 186kernel is not tickless, so the maximum sleep resolution is determined 187by the value of 188.Dv HZ 189which is by default 190.Dv 100 191in most architectures. 192This means that a request to sleep for less than 193.Dv 10ms 194(1 / 195.Dv HZ 196seconds), is rounded up to that. 197