You are not logged in.

#1 2012-08-08 21:32:15

kdar
Member
From: Alabama!!!
Registered: 2009-06-27
Posts: 352

How does sleep command works?

Does anyone know how sleep command works?

If I send "sleep 2m" to terminal.. would cpu be idling during those 2 minutes (from that command itself)? Is there something that is implemented in kernel that tells how sleep command works? and how its implemented in code?

Last edited by kdar (2012-08-08 21:39:54)

Offline

#2 2012-08-08 21:56:35

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,838

Re: How does sleep command works?

Offline

#3 2012-08-09 01:34:45

kdar
Member
From: Alabama!!!
Registered: 2009-06-27
Posts: 352

Re: How does sleep command works?

ah thanks.

Offline

#4 2012-08-22 06:01:28

paranoos
Member
From: thornhill.on.ca
Registered: 2004-07-22
Posts: 442

Re: How does sleep command works?

the sleep command doesn't do anything to your cpu. all it does is pause the shell for the specified length of time. it is handy for scripting mostly.

for instance, if you ran the following in your shell:

sleep 5; echo hello

the shell will wait for 5 seconds before printing "hello"

the important point is that other programs and processes are still running, and you can still run other commands on another shell during the sleep time. you can also "ctrl-c" to cancel sleep while it's waiting.

Last edited by paranoos (2012-08-22 06:04:31)

Offline

#5 2012-08-22 07:13:41

AaronBP
Member
Registered: 2012-08-06
Posts: 140
Website

Re: How does sleep command works?

Looks like the real work is done in gnulib: http://git.savannah.gnu.org/cgit/gnulib … anosleep.c

It looks like a loop that just continuously counts the number of nanoseconds that have passed to see if you've reached the requested time.

Actually keeping track of nanoseconds is handled by timespec. Doesn't look like a lot of code is involved, but I can't follow it. I'd be interested to hear an explanation!

Offline

#6 2012-08-22 08:35:25

portix
Member
Registered: 2009-01-13
Posts: 757

Re: How does sleep command works?

sleep from coreutils basically calls nanosleep which is a system call, you can find the source in the kernel sources (link).

Offline

Board footer

Powered by FluxBB