views:

324

answers:

5

I know an infinite loop creates a CPU problem. But, I don't quite understand why. Can anyone explain that to me.

Thanks!

+1  A: 

If the processor is working very hard (e.g. at 100%) for an extended period of time, heat dissipation problems can shorten it's life expectancy.

With a modern, well-ventilated CPU with a proper cooling fan operating under reasonable conditions (e.g. not in summer in the desert at high noon), this should not be a major concern.

Eric J.
A properly designed system should be able to run at 100% CPU usage indefinitely, without overheating. That said, it will use more power (a problem especially for laptops running off battery) and of course not all systems are properly designed...
Jeremy Friesner
@Jeremy: Agree. In fact grid computing solutions such as BOINC run all cores at 100% if you let them and I have never heard of issues. Not sure if your comment was before or after my edit/elaboration. Still, every bit of extra heat does shorten the lifetime. Just with a modern processor you're likely to replace it before head degradation causes it to fail if you're just talking about a few degrees. Serious overclockers do worry about heat dissipation and CPU life.
Eric J.
People who care about the life of their CPUs should not overclock them ... period.
Stephen C
I'm taking a stab in the dark here, but I don't think he literally meant "trash" the CPU, as in damage it.
dancavallaro
That's why I responded about heat damage. Not sure why I got downvoted because, if he's being literal, my answer is 100% correct and the only correct one at that.
Eric J.
@Eric. Perhaps you should also talk about the HLT instruction (at least for the x86 family) in your answer.
Moron
Sorry, when I wrote "trash the CPU", I don't mean the physical damage, just being literal. Thanks everyone for the answers, they are helpful.
+5  A: 

Infinite loops are no different than any other code running. The computer doesn't know that the infinite loop isn't a complicated calculation that just requires a lot of iterations.

Unless the infinite loop contains code that calls some system functions that yield time back to the OS, the OS treats it as a process that is actively working on something and gives it time to execute. If no other processes are running, it will eat up 100% of the CPU (on a single core system).

NilObject
Actually in modern general purpose O/S, any sane code will go into an interruptable wait state which stops the kernel from executing anything on that thread until it needs to.
Longpoke
Agreed, but my comparison was with an intense calculation, such as ones done for academic research.
NilObject
+11  A: 

The CPU cannot do anything else while it's executing that loop (which never ends). Even if you're using a pre-emptive multi-tasking system (so that infinite loop will only clog forever its own process or thread), the loop will "eat" its time slice each time the OS's pre-emptive scheduler hands it the CPU for the next slice -- doing nothing, but eating up one slice's worth of CPU time each and every time, so that much CPU is lost to all other threads which could otherwise be doing useful work.

Alex Martelli
+2  A: 

Infinite loops in themselves aren't a problem at all. Most applications that interact with a user are infinite loops. They repeatedly wait for user, act on it, and perform the cycle again. The operating system itself is an infinite loop. These kinds of infinite loop are said to be 'productive' because despite repeating something indefinitely, they periodically output something useful to the user.

I guess your concern is with unproductive infinite loops. But it's clear why these are a problem. They have all of the disadvantages of productive loops (consume power, use CPU time and so on) with none of the advantages ie. they don't produce anything useful.

A: 

You are probably referring to the Halt and Catch Fire instruction

dar7yl