views:

301

answers:

5

I am just interested how sleep(time in ms) is implemented in C lib or basically at the os level..
I am guessing ..
1) may be the based on the processor speed you do a while loop of nop's ( not sure if the sleep time will be accurate)..

2) any special register in processor , where u write some value and the processor simply halts for specified time ( this would be very in efficient as the processor can't run even other programs)

any clues? probably C Lib source code can explain??? I am not too particular about how "C" is implementing it.. Just wondering in general how "sleep()" function is implemented..

+8  A: 

Sleep() is implemented at the OS level. The processor doesn't spin when a task/thread/process is sleeping. That particular thread is put on a pending queue (the thread isn't ready to run) until the time has expired at which point the thread will be placed on the ready to run queue.

In the meantime, other threads that are ready to run will be run.

Only if no threads are ready to run will the OS go into the idle thread, which in generally issues instructions to shutdown (or put into a low-power state anyway) the processor until an hardware interrupt occurs.

Only for a very simple system (like the most simple of embedded systems), might Sleep() actually be implemented as nothing more than a busy wait loop.

Any operating system textbook, such as "Modern Operating Systems" by Tanenbaum will cover this in great detail - pretty much any of them (even an old, cheap, used one).

Michael Burr
ahhh.. so its not guaranteed to wake up after the time out.. Its up to the scheduler or basically other tasks in the system...??
FatDaemon
How soon after the timeout expires the task will get to run again is dependent on the scheduler. The system may guarantee that it'll run as soon as the timeout expires, but I think most will simply place it at the appropriate place in the ready-to-run queue (which might be at the front if the thread priority is greater than any other), adn it'll get run when it's next scheduled.
Michael Burr
Many embedded processors have a dedicated sleep instruction
mocj
@mocj -yes on re-reading, what I said about the idle thread wasn't as clear as I intended about halting the processor. I hope it's a little better now.
Michael Burr
It is guaranteed to NOT run until the timer is up.
Quinn Wilson
+2  A: 

The answer to your question is completely operating-system and implementation-dependent.

A simple way to think about it: When you call sleep(), the OS calculates the wakeup time, then sticks your process on a priority queue somewhere. It then just doesn't schedule your process to get any execution time until enough real time has passed for it to get popped off the queue.

Carl Norum
+1  A: 

You don't do any while loops, otherwise the system won't be able to do anything - not respond to the mouse, keyboard, network, etc.

Usually what most operating systems do is you add the delay to the current timestamp to get the timestamp when the task that requested the delay will be resumed (assuming there is no higher priority task running at that time) and add the [wakeupTimestamp, task pointer] to a list that is sorted by ascending by the timestamp. After that, the OS performs a context switch and runs the next available task. Periodically, the system compares the earliest timestamp on the sleepy list with the current timestamp, and if the deadline has passed, it moves the sleeping task into the "ready" task queue.

florin
+1  A: 

In a typical operating system, sleep calls into the kernel, which sets the process to wait until the specified amount of time is elapsed, and then goes and finds some other process to run. Absent anything better to do, it will run the 'idle process'. Once the time elapses, the scheduler will note that the sleeping process is good to go, and it will schedule it again.

bmargulies
+1  A: 

Sleep blocks your task/thread for the time value passed. Your task becomes un-runnable for that period or until something else interesting happens (like a signal), whichever is sooner.

It is not uncommon for sleep to call select() and pass no descriptors to wait on and a timeout value equal to your sleep period.

The system may implement this by setting a timer to expire after the time passed and then waiting on a semaphore that will be signalled when that timer expires. Thus it is blocked on that semaphore.

Southern Hospitality