# nanosleep

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           nanosleep — high resolution sleep
    


## SYNOPSIS



           #include <time.h>
    
           int nanosleep(const struct timespec *_rqtp_ , struct timespec *_rmtp_);
    


## DESCRIPTION



           The _nanosleep_() function shall cause the current thread to be
           suspended from execution until either the time interval specified
           by the _rqtp_ argument has elapsed or a signal is delivered to the
           calling thread, and its action is to invoke a signal-catching
           function or to terminate the process. The suspension time may be
           longer than requested because the argument value is rounded up to
           an integer multiple of the sleep resolution or because of the
           scheduling of other activity by the system. But, except for the
           case of being interrupted by a signal, the suspension time shall
           not be less than the time specified by _rqtp_ , as measured by the
           system clock CLOCK_REALTIME.
    
           The use of the _nanosleep_() function has no effect on the action or
           blockage of any signal.
    


## RETURN VALUE



           If the _nanosleep_() function returns because the requested time has
           elapsed, its return value shall be zero.
    
           If the _nanosleep_() function returns because it has been
           interrupted by a signal, it shall return a value of -1 and set
           _errno_ to indicate the interruption. If the _rmtp_ argument is non-
           NULL, the **timespec** structure referenced by it is updated to
           contain the amount of time remaining in the interval (the
           requested time minus the time actually slept). The _rqtp_ and _rmtp_
           arguments can point to the same object. If the _rmtp_ argument is
           NULL, the remaining time is not returned.
    
           If _nanosleep_() fails, it shall return a value of -1 and set _errno_
           to indicate the error.
    


## ERRORS



           The _nanosleep_() function shall fail if:
    
           **EINTR** The _nanosleep_() function was interrupted by a signal.
    
           **EINVAL** The _rqtp_ argument specified a nanosecond value less than
                  zero or greater than or equal to 1000 million.
    
           _The following sections are informative._
    


## EXAMPLES



           None.
    


## APPLICATION USAGE



           None.
    


## RATIONALE



           It is common to suspend execution of a thread for an interval in
           order to poll the status of a non-interrupting function. A large
           number of actual needs can be met with a simple extension to
           _sleep_() that provides finer resolution.
    
           In the POSIX.1‐1990 standard and SVR4, it is possible to implement
           such a routine, but the frequency of wakeup is limited by the
           resolution of the _alarm_() and _sleep_() functions. In 4.3 BSD, it is
           possible to write such a routine using no static storage and
           reserving no system facilities. Although it is possible to write a
           function with similar functionality to _sleep_() using the remainder
           of the _timer_*_() functions, such a function requires the use of
           signals and the reservation of some signal number. This volume of
           POSIX.1‐2017 requires that _nanosleep_() be non-intrusive of the
           signals function.
    
           The _nanosleep_() function shall return a value of 0 on success and
           -1 on failure or if interrupted. This latter case is different
           from _sleep_().  This was done because the remaining time is
           returned via an argument structure pointer, _rmtp_ , instead of as
           the return value.
    


## FUTURE DIRECTIONS



           None.
    


* * *