Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

sleep - suspend process for fixed interval of time

&pagelevel(4)&pagelevel

Syntax

#include <unistd.h>

unsigned int sleep(unsigned int seconds); 

Description

sleep() causes the current process to be suspended from execution until either the number of real-time seconds specified by seconds has elapsed or a signal is delivered to the calling process, and the action of that signal is to invoke a signal-handler or to terminate the process. The actual suspension time may be longer than seconds for priority reasons (i.e. due to the scheduling of other activity by the system).

If a SIGALRM signal is generated for the calling process during execution of sleep() and if the SIGALRM signal is being ignored or blocked from delivery, it is undefined whether sleep() will return when the signal is processed.

If the signal is blocked, it is likewise undefined whether it will still be pending after sleep() returns or whether it will be discarded.

If a SIGALRM signal is generated for the calling process during the execution of sleep(), except as a result of a prior call to alarm(), and if the SIGALRM signal is not being ignored or blocked from delivery, it is undefined whether that signal will have any effect other than forcing sleep() to return.

If sleep() is interrupted by a signal handler, the results are undefined under the following conditions:

  • if the signal handler examines or changes the time at which a SIGALRM signal is to be generated

  • if the signal handler changes the action associated with the SIGALRM signal

  • if the signal handler changes whether the SIGALRM signal is to be blocked from delivery

If a signal handler interrupts sleep() and calls siglongjmp() or longjmp() to restore an environment saved prior to the sleep() call, both the action associated with the SIGALRM signal and the time at which the signal is to be generated are undefined. It is likewise undefined whether the SIGALRM signal will be blocked if the signal mask of the process is not also restored as part of the environment (see also sigsetjmp()).

If threads are used, then the function affects the process or a thread in the following manner: sleep() causes the current thread to be suspended until the specified time has expired or until a signal is sent to the thread.

Return val.

0

if sleep() returns because the specified time has elapsed. 


seconds minus the time already spent sleeping, i.e. the unslept time in seconds



if sleep() returns because it was terminated prematurely by the delivery of a signal. 


sleep() is always successful.

Notes

Although the program is suspended by sleep(), time continues to run for a previously set alarm clock (see alarm()). This has the following effects:

  1. If the previously set alarm time is less than the sleep time, e.g.:

    alarm(2);

    sleep(30);

    the alarm is triggered and the sleep call is ended after two "sleep" seconds have elapsed.

  2. If the previously set alarm time is greater than the sleep time, e.g.:

    alarm(30);

    sleep(5);

    time continues to run on the alarm clock for 5 "sleeping" seconds. Following the sleep call, the alarm clock will be set at 25.

The time for which the program is actually suspended may also deviate from seconds for the following reasons:

  • it may be up to one second shorter because "awakening" takes place at fixed 1-second intervals;

  • it may be longer by any amount for priority reasons because the system has "more important" things to do. 

See also

alarm(), pause(), sigaction(), unistd.h