Sleep (operating system)
   HOME

TheInfoList



OR:

A
computer program A computer program is a sequence or set of instructions in a programming language for a computer to execute. Computer programs are one component of software, which also includes documentation and other intangible components. A computer program ...
(
process A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management *Business process, activities that produce a specific se ...
, task, or thread) may sleep, which places it into an inactive state for a period of time. Eventually the expiration of an interval
timer A timer is a specialized type of clock used for measuring specific time intervals. Timers can be categorized into two main types. The word "timer" is usually reserved for devices that counts down from a specified time interval, while devices th ...
, or the receipt of a
signal In signal processing, a signal is a function that conveys information about a phenomenon. Any quantity that can vary over space or time can be used as a signal to share messages between observers. The ''IEEE Transactions on Signal Processing'' ...
or
interrupt In digital computers, an interrupt (sometimes referred to as a trap) is a request for the processor to ''interrupt'' currently executing code (when permitted), so that the event can be processed in a timely manner. If the request is accepted, ...
causes the program to resume execution.


Usage

A typical ''sleep''
system call In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
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

On Windows, the Sleep() function takes a single parameter of the number of milliseconds to sleep. The Sleep() function is included in
kernel32.dll The Microsoft Windows operating system supports a form of shared libraries known as "dynamic-link libraries", which are code libraries that can be used by multiple processes while only one copy is loaded into memory. This article provides an ove ...
. The function has a resolution no higher than the current timer resolution, typically 16ms but at minimum 1ms, adjustable via the family of "media timer" APIs. For higher precisions, it is necessary to use a
busy loop In computer science and software engineering, busy-waiting, busy-looping or spinning is a technique in which a process repeatedly checks to see if a condition is true, such as whether keyboard input or a lock is available. Spinning can also be u ...
over , such as the one used in
gnulib Gnulib, also called the GNU portability library, is a collection of software subroutines which are designed to be usable on many operating systems. The goal of the project is to make it easy for free software authors to make their software run ...
.


Unix

On
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
and other
POSIX The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
operating systems, the sleep() function is called providing a single parameter of type unsigned integer of the number of seconds to sleep. A higher-precision version is the function and the now deprecated . POSIX also allows for choosing clock sources via the extended version . A version of was proposed to be part of
the C programming language ''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
, but was rejected. The UTC time part of the same proposal was added to
C11 C11, C.XI, C-11 or C.11 may refer to: Transport * C-11 Fleetster, a 1920s American light transport aircraft for use of the United States Assistant Secretary of War * Fokker C.XI, a 1935 Dutch reconnaissance seaplane * LET C-11, a license-build var ...
.


C examples

In
Windows API The Windows API, informally WinAPI, is Microsoft's core set of application programming interfaces (APIs) available in the Microsoft Windows operating systems. The name Windows API collectively refers to several different platform implementations th ...
: Sleep(2 * 1000); // Sleep for 2 seconds In Unix or POSIX system calls: sleep(2); // Sleep for 2 seconds


Low level functionality

Sleep causes the thread or process to give up the remainder of its time slice and stay in the ''Not Runnable'' state for the specified duration. While there is generally a guarantee for the minimum time period, there is no strict guarantee that the thread will run immediately or soon, or even at all, once the specified time has passed. It is up to the scheduler's discretion, and dependent on thread priorities and implementation details such as timer resolutions when the sleeping thread will run again. On POSIX systems, the ''nanosleep'' and related syscalls are interruptible by signals, returning the remaining sleep time. The ''sleep'' library function, on the other hand, is implemented via the ''alarm'' syscall on many older systems, thus it only works ''by delivering a signal''. The Windows Sleep function is non-interruptible due to absence of signals (other than the thread or its process being terminated), although the related SleepEx function can be used to put the thread into an alertable state, allowing APC calls being made while the thread is sleeping. Also, a thread can technically be "interrupted" in case e.g. the process terminates due to an exception in a different thread.


Uses

Some system programs that never terminate execute an
event loop In computer science, the event loop is a programming construct or design pattern that waits for and dispatches events or messages in a program. The event loop works by making a request to some internal or external "event provider" (that generally ...
, 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 Poll, polled, or polling may refer to: Figurative head counts * Poll, a formal election ** Election verification exit poll, a survey taken to verify election counts ** Polling, voting to make decisions or determine opinions ** Polling places o ...
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. The sleep() function call can be repeatedly called for short periods of time to slow the execution of a running program or code. Throttling code in this manner provides a coarse mechanism for mitigating the effects of overheating hardware or easing timing issues for legacy programs. The downside to cycling sleep and running states rather than leveraging cycle emulation (via an emulator) to control the execution speed of software is that interactive software will acquire a notable stutter if too little time is spent awake, too much time is spent sleeping, or a combination of both.


Uninterruptible sleep

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. Such processes cannot be killed even with SIGKILL and the only non-sophisticated way to get rid of them is to reboot the system.


See also

*
Signal In signal processing, a signal is a function that conveys information about a phenomenon. Any quantity that can vary over space or time can be used as a signal to share messages between observers. The ''IEEE Transactions on Signal Processing'' ...
*
System time In computer science and computer programming, system time represents a computer system's notion of the passage of time. In this sense, ''time'' also includes the passing of days on the calendar. System time is measured by a ''system clock'', w ...
*
sleep (command) In computing, sleep is a command in Unix, Unix-like and other operating systems that suspends program execution for a specified time. Overview The sleep instruction suspends the calling process for at least the specified number of seconds (the ...
*
Sleep mode Sleep mode (or suspend to RAM) is a low power mode for electronic devices such as computers, televisions, and remote controlled devices. These modes save significantly on electrical consumption compared to leaving a device fully on and, upon resu ...
*
wait (system call) In computer operating systems, a process (or task) may wait on another process to complete its execution. In most systems, a parent process can create an independently executing child process. The parent process may then issue a ''wait'' system ...


References

{{DEFAULTSORT:Sleep (Operating System) Process (computing) Threads (computing)