This question is related to linux scheduling and processor activity with different phases in a thread which may be doing something actively, or idle/waiting or may be sleeping. When a thread is actively doing something, the processor will be executing instruction (and processor throughput i.e. no of instruction it is executing per sec) will be high. My questing is how does the processor behave (may be roughly) when a thread is waiting vs when it is sleeping? Can a waiting thread be sleeping? When a thread is sleeping, does it mean the processor is idle? When a processor is idle, does it mean all the thread are sleeping? When a processor is idle, does Linux literally put the processor in a mode such that it does not execute any instructions (i.e. clock gated)?
-
Possible duplicate: http://stackoverflow.com/questions/1719071/how-is-sleep-implemented-at-os-level/1719092#1719092 – n0p Aug 29 '14 at 08:43
-
`When a thread is sleeping, does it mean the processor is idle?` - just to mention about `the processor is idle`. When _your process is sleeping_ Linux kernel is actually working handling interrupts and doing lots of stuff. So I think you have to consider Linux kernel activity as well as activity of your threads. – Aug 29 '14 at 08:50
1 Answers
When a thread is sleeping it is actually waiting for the OS to put it into the execution queue. Sleeping occurs when a thread asks the OS for interrupting the thread itself for some amount of time. It is normally achieved by using functions as sleep()
. The OS removes the thread from the execution queue for the requested time and when it's over, it continues running the thread. Other I/O functions act in a similar way: if a thread calls read()
, the OS will remove the thread from the execution queue until there's data in the resource being read.
On the other hand, a processor is idle when it has no code to run nor any hardware events to attend. The idle state is a hardware state: the CPU is not running code, just waiting for some event to occur (i.e.: scheduling timer or I/O interrupt) that signals the processor to run code. A 8086 processor can achieve the idle state by executing the HLT
(halt) instruction. After running HLT
the processor stops running code until a hardware interrupt (like the ones listed above) is received.

- 5,224
- 17
- 30