Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Usage  



1.1  Windows  





1.2  Unix  





1.3  C examples  







2 Low level functionality  





3 Uses  



3.1  Uninterruptible sleep  







4 See also  





5 References  














Sleep (system call)






Čeština
فارسی
Simple English

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 

(Redirected from Sleep (operating system))

Acomputer 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 signalorinterrupt causes the program to resume execution.

Usage

[edit]

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]

On Windows, the Sleep() function takes a single parameter of the number of milliseconds to sleep. The Sleep() function is included in kernel32.dll. [1]

The Sleep() function has a resolution no higher than the current timer resolution, typically 16ms but at minimum 1ms, adjustable via the timeBeginPeriod() family of "media timer" APIs. For higher precisions, it is necessary to use a busy loop over QueryPerformanceCounter(), such as the one used in gnulib.[2]

Unix

[edit]

OnUnix-like and other POSIX operating systems, the sleep() function is called providing a single parameter of type unsigned integer of the number of seconds to sleep.[3] A higher-precision version is the nanosleep() function and the now deprecated usleep.[4] POSIX also allows for choosing clock sources via the extended version clock_nanosleep().[5]

A version of clock_nanosleep() was proposed to be part of the C programming language, but was rejected. The UTC time part of the same proposal was added to C11.[6]

C examples

[edit]

InWindows API:

Sleep(2 * 1000);  // Sleep for 2 seconds

In Unix or POSIX system calls:

sleep(2);       // Sleep for 2 seconds

Low level functionality

[edit]

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

[edit]

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.

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[7] 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.[8]

Uninterruptible sleep

[edit]

An uninterruptible sleep state is a sleep state that will not 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.[9] Such processes cannot be killed even with SIGKILL and the only non-sophisticated way to get rid of them is to reboot the system.[10][11]

See also

[edit]

References

[edit]
  • ^ "winapi - Is there a Windows equivalent of nanosleep?". Stack Overflow.
  • ^ sleep(3p) – Linux Manual
  • ^ nanosleep(3p) – Linux Manual
  • ^ clock_nanosleep(3p) – Linux Manual
  • ^ Kuhn, Markus. "Modernized API for ISO C". www.cl.cam.ac.uk.
  • ^ mion (2016-12-06). "BES – Battle Encoder Shirase 1.6.3 (stable) & 1.7.4 for Windows 7/XP/2000". mion.faireal.net. Retrieved 2017-02-09.
  • ^ Marletta, Angelo (2015-03-12). "CPULIMIT". GitHub. Retrieved 2017-02-09.
  • ^ "top(1) - Linux manual page". man7.org. 2016-12-12. Retrieved 2017-02-09.
  • ^ "Processes in an Uninterruptible Sleep (D) State". Novell. 2009-02-21. Retrieved 2017-02-09.
  • ^ Fusco, John (2007-03-06). The Linux Programmer's Toolbox. Pearson Education. ISBN 9780132703048.

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Sleep_(system_call)&oldid=1165808940"

    Categories: 
    Process (computing)
    Threads (computing)
    Hidden categories: 
    Articles with short description
    Short description is different from Wikidata
     



    This page was last edited on 17 July 2023, at 15:07 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki