C Programming/POSIX Reference/unistd.h/sleep

From Wikibooks, open books for an open world
Jump to navigation Jump to search

A computer program (process, task, or thread) may sleep, which places it into an inactive state for a period of time. Eventually the expiration of an interval timer, or the receipt of a signal or interrupt causes the program to resume execution.

Usage[edit | edit source]

A typical sleep system call takes a time value as a parameter, specifying the minimum amount of time that the process is to sleep before resuming execution. The parameter typically specifies seconds, although some operating systems provide finer resolution, such as milliseconds or microseconds.

Windows[edit | edit source]

On Windows 2000 and newer, the Sleep() function takes a single parameter of the number of milliseconds to sleep. [1] The sleep() function is included in the kernel32.dll, but no sleep command (executable) is natively available for scripts (batch files). It can be found in collections of Windows utilities like Windows 2003 Resource Kit. [2]

Unix[edit | edit source]

On Unix-like operating systems, the sleep() function is called providing a single parameter of type unsigned integer of the number of seconds to sleep. [3] (For more precise sleep times one can use the usleep() function.) [4]

C examples[edit | edit source]

In Windows OS:

while (myInt <= 100)
    Sleep(2*1000);  // Sleep for 2 seconds

In Unix:

while (myInt <= 100)
    sleep(2);       // Sleep for 2 seconds

C++ Example[edit | edit source]

#include <windows.h> // Windows.h is necessary for Sleep function
sleep(3000) // In milliseconds

Low level functionality[edit | edit source]

Sleep causes the thread or process to enter the Not Runnable state. This allows the CPU to suspend the thread or process and continue executing other threads or processes until the sleep has finished, and the thread or process is allowed to continue executing. On Windows systems the sleep system call is non-interruptible, which differs from Wait and SleepEx system calls, which can be interrupted.[5][6] However, some functions labeled sleep() are alterable by design[7]. On Unix system signals interrupt system calls, including sleep().[8] Sleep often leads to poor code design and the wait or nanosleep functions are preferable.

Uses[edit | edit source]

Some system programs that never terminate execute an event loop, going to sleep at the start of each cycle and waiting for some event to awaken them. Once an event is received, the program services the event, then returns to the beginning of the next wait cycle.

Other programs periodically poll for events by going to sleep and resuming execution after a specific interval of time. Once execution is resumed, the program polls for events or status changes, and then services any that occurred while it was asleep. After servicing the events, the program then goes to sleep again for the next time interval. Certain kinds of heartbeat events or keep-alive signals can be generated by these kinds of programs.

Uninterruptible sleep[edit | edit source]

An uninterruptible sleep state is a sleep state that won't handle a signal right away. It will wake only as a result of a waited-upon resource becoming available or after a time-out occurs during that wait (if specified when put to sleep). It is mostly used by device drivers waiting for disk or network IO (input/output). When the process is sleeping uninterruptibly, signals accumulated during the sleep will be noticed when the process returns from the system call or trap.

In Unix-like systems the command 'ps -l' uses code "D" for the uninterruptible sleep state of a process.

See also[edit | edit source]

References[edit | edit source]