# # Unix/Linux process sleep
APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

2003/11/13 Unix/Linux sleep

I've removed advertising from most of this site and will eventually clean up the few pages where it remains.

While not terribly expensive to maintain, this does cost me something. If I don't get enough donations to cover that expense, I will be shutting the site down in early 2020.

If you found something useful today, please consider a small donation.



Some material is very old and may be incorrect today

© November 2003 Tony Lawrence

There are two aspects to sleep: a voluntary sleep, where a process needs to delay for whatever purpose and deliberately calls "sleep" with some time argument, and involuntary, where the kernel scheduling code puts the process to sleep either because some resource it wants is slow, or unavailable, or because it's just time for some other process to use the CPU. There are two aspects to sleep: a voluntary sleep, where a process needs to delay for whatever purpose and deliberately calls "sleep" with some time argument, and involuntary, where the kernel scheduling code puts the process to sleep either because some resource it wants is slow, or unavailable, or because it's just time for some other process to use the CPU.

We'll talk about voluntary sleeps here. First, there is always a limit to how long you can sleep. That's probably going to be limited by the size of an unsigned integer, and therefore your longest possible sleep will be that many seconds. That's a long, long time though - more than any program would ever need.

The shortest possible sleep is 1 second, so programs that need shorter resolution have to use something other than "sleep". Some systems have calls like "nap" for this.

At the shell, "sleep 10" goes awy for 10 seconds. This is useful for loops:

while true
do
ls -l
echo "sleeping"
sleep 10
done
 

Sleeps can of course be interrupted by signals, which causes it to end earlier than expected, and of course a busy system can cause the sleep to run longer than your intention. Just because you say "sleep 10" does not mean your next statement will be exactly 10 seconds later. C programs use "sleep(10)" in the same way.

Sleep can be internally implemented using "alarm()", and if it is, there are gray areas: what happens if you set an alarm to go off in ten seconds (the alarm will generate a signal 10 seconds from now) and do a 5 second sleep a second or so later? Is the alarm still set? Maybe, but it's system dependent.

See also Higher resolution timers in the shell.


If you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Unix/Linux process sleep


Inexpensive and informative Apple related e-books:

Photos: A Take Control Crash Course

Take control of Apple TV, Second Edition

iOS 10: A Take Control Crash Course

Are Your Bits Flipped?

Take Control of Upgrading to El Capitan





More Articles by © Tony Lawrence





Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





One in a million is next Tuesday. (Gordon Letwin)




Linux posts

Troubleshooting posts


This post tagged:

Scripting

Shell

UnixWords



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode