nanosleep Subroutine

Purpose

Causes the current thread to be suspended from execution.

Library

Standard C Library (libc.a)

Syntax

#include <time.h>

int nanosleep (rqtp, rmtp)
const struct timespec *rqtp;
struct timespec *rmtp;

Description

The nanosleep subroutine causes the current thread to be suspended from execution until either the time interval specified by the rqtp parameter 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. This can also occur because of the scheduling of other activity by the system. Unless it is interrupted by a signal, the suspension time will not be less than the time specified by the rqtp parameter, as measured by the system clock CLOCK_REALTIME.

The use of the nanosleep subroutine has no effect on the action or blockage of any signal.

Parameters

Item Description
rqtp Specifies the time interval that the thread is suspended.
rmtp Points to the timespec structure.

Return Values

If the nanosleep subroutine returns because the requested time has elapsed, its return value is zero.

If the nanosleep subroutine returns because it has been interrupted by a signal, it returns -1 and sets errno to indicate the interruption. If the rmtp parameter is non-NULL, the timespec structure is updated to contain the amount of time remaining in the interval (the requested time minus the time actually slept). If the rmtp parameter is NULL, the remaining time is not returned.

If the nanosleep subroutine fails, it returns -1 and sets errno to indicate the error.

Error Codes

The nanosleep subroutine fails if:
Item Description
EINTR The nanosleep subroutine was interrupted by a signal.
EINVAL The rqtp parameter specified a nanosecond value less than zero or greater than or equal to 1000 million.