Re: newbie questions



Ask wrote:
Josef Moellers wrote:

Ask wrote:

c) Is there any difference between delay and sleep from scheduling
perspective ?

Yes: a delay does not cause a rescheduling while a sleep does.
There is also a fundamental difference between delay and sleep: a delay
is for a certain period of time while a sleep is waiting for an event.

A delay could be implemented as a busy wait loop until the time stamp
counter has moved past a certain point while a sleep is implemented as
saving the current process' state such that it can be found when the
event occurs and a rescheduling operation is then done.



I have seen "sleep" at 3 places
- OS books-> sleep is waiting for an event
- Unix command
- ANSI C Library

What are the differences ? Is the Unix command "sleep" and C Library
call actually a delay ? If not, how are they typically implemented -
using timers and interrupts ?

Difference: the first is referring to an OS-internal operation while the latter two refer to user-land operations (which will be implemented using kernel facilities).
I would be the last to deny that naming a kernel facility "sleep" which does something else than a user-land facility "sleep" is probably done to confuse the enemy. I also have trouble with facilities "wait" and "signal" while the CS community has a different notion of "wait" and "signal" ;-)

The sleep(1) command is sometimes built-in to a shell, but whther or not it is a stand-alone utility, it is implemented using the sleep(3) C library call or maybe directly the nanosleep(2) system call.
The former used to be implemented by an alarm(2)/pause(2) combination but is now implemented using one system call (nanosleep(2)).
You can use strace and ltrace to find out how they are implemented.

How nanosleep(2) is implemented can be read in the kernel sources.

(Please note that in the following I distinguish between "delaying execution" and the "delay" concept discussed previously!)

Mostly you delay execution in user space for a lengthy period of time (you rarely delay execution by only a few ms, besides multitasking will induce execution-delays anyway, so waiting for only a few ms might get drowned by execution-delays induced by multitasking), so implementing this as a busy-wait "delay" would be a waste of processor power and also of electric power as busy-waiting will keep the processor executing instructions while putting a process/thread into some queue might permit you to actually halt the processor because no other process/thread is ready for execution.

That said: it may very well be that nanosleep(2) might distinguish between veeeeeery short sleeps, where the overhead of going to sleep and waking up would exceed the desired period of execution-delay, and longer sleeps where this is not the case, and implement short execution-delays by a busy-waiting "delay", but this could be hidden inside the C-library interface (where means must exist to actually determine how to "delay" for a specific period of time) or within the kernel itself, where some of the overhead would already have been spent.
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

.



Relevant Pages

  • Re: I NEED HELP
    ... order the delay, and if Julie hardly tops it too, the pool will ... sleep unlike the competent rock. ...
    (sci.crypt)
  • Re: IHTMLDocument3 getElementById fails but not when I interpose a messagebox
    ... > replicate the delay in other ways and it didn't behave ... Sleep() does not allow window messages to be processed. ... m_MyView is the one that's derived from IDispEventImpl, ... cookie, so when you try to advise it a second time, it asserts. ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Basic thread questions
    ... several software-hardware interfaces. ... loop with a sleep of equal duration (on the theory that sleep both ... When I adjust the test app's sleep to a 500ms delay, ...
    (comp.programming.threads)
  • Re: Making a program pause
    ... run a for loop that waits until 5 minutes has passed to continue down ... How can I implement a delay, or time a user's response, ... setitimer, sleep(), Sleep, times, and usleep. ... only clock() is part of the ANSI Standard. ...
    (comp.lang.c)
  • Re: To reduce the speed of execution of a program VBAEXCEL
    ... I have made a program which circulates in a range of cells. ... I would like to reduce the speed of execution. ... assuming that you're using a loop to do your task (my ... Dim delay As Long ...
    (comp.lang.basic.visual.misc)