Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does endless While loop take up CPU resources?

Tags:

c++

c

linux

daemon

From what I understand, you write your Linux Daemon that listens to a request in an endless loop.
Something like..

int main() {     while(1) {         //do something...     } } 

ref: http://www.thegeekstuff.com/2012/02/c-daemon-process/

I read that sleeping a program makes it go into waiting mode so it doesn't eat up resources.

1.If I want my daemon to check for a request every 1 second, would the following be resource consuming?

int main() {     while(1) {         if (request) {             //do something...         }         sleep(1)     } } 

2.If I were to remove the sleep, does it mean the CPU consumption will go up 100%?

3.Is it possible to run an endless loop without eating resources? Say..if it does nothing but just loops itself. Or just sleep(1).

Endless loops and CPU resources is a mystery to me.

like image 211
resting Avatar asked Dec 03 '12 09:12

resting


People also ask

Does while loop consume CPU?

Your program won't consume any CPU resources while sleeping. Even sleeping for just some milliseconds makes things smooth in an endless loop like you've shown it.

Can infinite loops break your computer?

No. Unless the application has left behind execution artifacts (such as other zombie processes which happen to loop too), nothing will happen (after the execution of the process stops, the operating system reclaims all the resources it held).

What are the dangers of an endless loop?

An infinite loop is a piece of code that keeps running forever as the terminating condition is never reached. An infinite loop can crash your program or browser and freeze your computer.

What will happen if an infinite while loop runs?

An infinite loop, as the name suggests, is a loop that will keep running forever. If you accidentally make an infinite loop, it could crash your browser or computer. It is important to be aware of infinite loops so you can avoid them.


2 Answers

The poll and select calls (mentioned by Basile Starynkevitch in a comment) or a semaphore (mentioned by Als in an answer) are the correct ways to wait for requests, depending on circumstances. On operating systems without poll or select, there should be something similar.

Neither sleep, YieldProcessor, nor sched_yield are proper ways to do this, for the following reasons.

YieldProcessor and sched_yield merely move the process to the end of the runnable queue but leave it runnable. The effect is that they allow other processes at the same or higher priority to execute, but, when those processes are done (or if there are none), then the process that called YieldProcessor or sched_yield continues to run. This causes two problems. One is that lower priority processes still will not run. Another is that this causes the processor to be always running, using energy. We would prefer the operating system to recognize when no process needs to be running and to put the processor into a low-power state.

sleep may permit this low-power state, but it plays a guessing game about how long it will be until the next request comes in, it wakes the processor repeatedly when there is no need, and it makes the process less responsive to requests, since the process will continue sleeping until the expiration of the requested time even if there is a request to be serviced.

The poll and select calls are designed for exactly this situation. They tell the operating system that this process wants to service a request coming in on one of its I/O channels but otherwise has no work to do. This allows the operating system to mark the process as not runnable and to put the processor in a low-power state if suitable.

Using a semaphore provides the same behavior, except that the signal to wake the process comes from another process raising the semaphore instead of activity arising in an I/O channel. Semaphores are suitable when the signal to do some work arrives in this way; simply use whichever of poll or a semaphore is more appropriate for your situation.

The criticism that poll, select, or a semaphore causes a kernel-mode call is irrelevant, because the other methods also cause kernel-mode calls. A process cannot sleep on its own; it has to call the operating system to request it. Similarly, YieldProcessor and sched_yield make requests to the operating system.

like image 182
Eric Postpischil Avatar answered Oct 09 '22 12:10

Eric Postpischil


Is it possible to run an endless loop without eating resources? Say..if it does nothing but just loops itself. Or just sleep(1).

There ia a better option.
You can just use a semaphore, which remains blocked at the begining of loop and you can signal the semaphore whenever you want the loop to execute.
Note that this will not eat any resources.

like image 38
Alok Save Avatar answered Oct 09 '22 13:10

Alok Save