– Why does daemon process dont spike cpu usage to 100% whereas an infinite for loop does ?

289 views

A daemon process is supposed to be running all the time waiting for user to do something and then respond right ? Does “All the time” mean infinite time ? (Infinite loop) ?

When I run an infinite for loop, the cpu usage spikes to 100% ? Why is it so ?

Thank you for your time.

In: 4

3 Answers

Anonymous 0 Comments

A daemon does not just run an infinite loop, it calls to operating system and asks “put me to sleep until X happens”. The OS then marks the process as “sleeping” and does not run it’s code anymore. If all processes are asleep, OS issues a HALT command to the CPU, making it power down until something happens or the timer runs out.

Then the X happens, OS will walk the list of waiting processes and mark them as “running” again. That means, they will get CPU time when their turn comes up.

Note: the only time the CPU usage <100% is then *all* processes are asleep! If even one is awake, it alone will get all 100% of CPU! That’s why your “busy-waiting” process makes CPU usage 100% – it never goes to sleep.

You are viewing 1 out of 3 answers, click here to view all answers.