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


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

Well it’s not trying to scan at your clock speed for one. It’s set to intervals. It’s not always actively processing either, many times it’s interrupt driven by certain actions.

If your loop doesn’t do anything that could possibly yield the CPU to other processes, then that loop is going to nail the cpu to 100%.

Proper way of coding the loop is to have it sleep for a brief time or wait for I/o to let the OS run other tasks.

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.