4

What will CPU do when a sleep(10) or equivalent statement is executed. How will it wait exactly for 60 seconds when CPU also does context switching a brings this process to wait state.

Craig S. Anderson
  • 6,966
  • 4
  • 33
  • 46
  • http://stackoverflow.com/questions/1858640/how-can-i-create-a-sleep-function-in-16bit-masm-assembly-x86 – Tymoteusz Paul Jul 13 '15 at 12:12
  • This does not include anything about scheduling delays @TymoteuszPaul can you please point me to any conversation with that if possible – user2144976 Jul 14 '15 at 05:25

1 Answers1

2

sleep function usually asks operating system to assign cpu to another process. Actually, there is a special process (usually named 'idle'), which gets the cpu if there are no other processes waiting for cpu. On intel processor, idle process executes special instruction (wait), which stops the processor (then, it does not execute more instructions). Whether is processor assigned to idle process or not, it still responds to hardware interrupts (special signals produced by other computer hardware in case the hardware would like to inform processor about some event). One of the interrupts source is a timer - it sends interrupt to cpu each 10ms or so (depends on concrete computer and operating system). As a response to the timer event, operating system may assign cpu back to the process, which executed the sleep - if the specified time elapsed.

As the operating system is able to measure time with precision of 1 timer tick, Your process will be ready in specified time +- 2 ticks. In case Your cpu is busy at the moment, Your process gets assigned the cpu with some additional delay.