0

I have a question with regard to the sleep function declared in unistd.h

Assume we use a CFS scheduler.

We have a process that is ready to run(lets call this "READY" state),it gets picked to run,and now is running(so called "RUNNING" state).

During its execution in the RUNNING state it encounters a sleep statement,say sleep(10) that makes it sleep for 10 seconds or until a signal gets delivered or whichever is sooner.

Now when sleep(10) is being executed,is the process in READY state or is it put back into its original priority in the RUNNING queue or is it put to the WAIT queue.

I am unable to visualize the correct sequence of events.One thought process suggests that it remains in the READY queue,while another thought is that its put to the WAIT queue waiting for a timer expiry of some sorts.

Please let me know how this would work,or if there is something wrong in my question. Thanks

hit.at.ro
  • 306
  • 3
  • 11

1 Answers1

1

I believe it depends on the duration of the sleep, i.e., if the wait is busy then it can be running, if the wait is long then it will be in the wait queue. Also, you should be able to confirm this by putting a process to a long sleep and checking its state.

perreal
  • 94,503
  • 21
  • 155
  • 181
  • sleep() or any variations (such as usleep()) is never a busy wait, at least not on any *nixes or other common OSs. They suspend the process to a wait state, the only special condition is sleep(0) which more or less just reschedules the process (i.e. it does not enter a wait state) – nos Mar 14 '13 at 00:43
  • I see . So after the specified sleep time its put back into the ready queue and upon the next schedule() call it gets scheduled if there is no higher priority process already ? – hit.at.ro Mar 14 '13 at 00:56
  • yes generally this is how it's implemented. but again, that all depends on the concrete implementation. – perreal Mar 14 '13 at 00:58