--- Lun 19/10/09, Christopher Coale <[email protected]> wrote: > Da: Christopher Coale <[email protected]> > Oggetto: Re: [c-prog] Infinite loop? > A: [email protected] > Data: Lunedì 19 ottobre 2009, 06:58 > > > Thomas Hruska wrote: > > > > I once ran a program that chugged 100% CPU, RAM, and most of a 20GB HD > for a good month. It was in the middle of January in the Midwest (it > was a miserable -20 Fahrenheit outside) and I had to have the windows > open and the heat turned off to keep the room temperature around 85 > Fahrenheit. People were literally begging me to turn off the program. > That computer still works fine. > > In another situation, I wrote a program similar to yours. Only I > changed the priority of the process to REALTIME_PRIORITY_ CLASS before > the while loop by calling SetPriorityClass( ). Upon running the program, > it instantly and completely froze Windows, forcing me to hit the reset > button on the box. There was no way to see what was going on, but the > program was surely spinning the CPU as fast as it could possibly go. No > damage done.
Modern CPUs run programs in two main "modes" (they are called RINGS by Intel - other CPU do have similar concepts): 1. user (ring 3) 2. kernel (ring 0) In user mode, no change on the priority class of a task _should_ freeze the OS: if windows froze than I think it shouldn't be called 'an Operating System'. Some parts of device drivers that run in kernel mode may actually freeze the system. > Da: Christopher Coale <[email protected]> > Oggetto: Re: [c-prog] Infinite loop? > A: [email protected] > Data: Lunedì 19 ottobre 2009, 06:58 > > > The CPU is, in fact, ALWAYS executing code (when powered on), no matter > what. Hence why x86 has the "hlt" instruction. Once the computer is > powered on, the BIOS is going to load your boot sector, the CPU will > jump to it, then the IP/EIP register will continually increment. This > means that no matter what memory value the IP/EIP register points to, > the CPU will treat it as an instruction, "execute" it, then increment > IP/EIP again, and so on... so yes, the CPU will always execute > SOMETHING. This is the reason why on Windows the "System Idle" process > always seems to use so much CPU - the CPU has to execute something, so > when no other thread needs a time slice, this thread/process gets it. This is true. CPU always executes _something_; the idle process, for the most time. On modern OSes and CPU, the idle process executes a HTL instruction if it detects that the scheduler is empty. HLT stops the CPU until the next interrupt. > Saying that running an empty-bodied loop will damage the CPU is silly. > An empty loop is in a sense 1 instruction (jmp <address of this jmp > instruction> ). If you are saying processes running at "100% CPU" is > damaging, then that is silly as well. The CPU itself will always be > running at 100% (unless the "hlt" instruction is executed, in which case > the CPU simply "stops"). And in case you're wondering, the "hlt" > instruction can't be executed by normal programs, only the OS kernel. Maybe, an infinite for-loop _may_ contribute to damage the CPU because of overheating. If you let the OS enter the idle loop than an HLT is executed which significantly reduces power consumption and heat. But if you leave the computer ON for weeks and never enter the idle process, the CPU will never be halted, and a poor cooling design may damage the system. Note that this has nothing to do with the CPU: the CPU can run for years if properly cooled. As Thomas wrote, an infinite for-loop could be a test for the whole system. > So, I guess, this extra long essay can be shortened into a simple > phrase: CPU usage is just a concept. ;) Well, what we see in the 'CPU usage window' is the 'percentage' of use. Suppose we have 10 tasks, 9 of which waiting for input (they entered the idle loop) and one task that executes an infinite for-loop. The system idle process is, actually, never executed for this task. The other nine tasks are in the idle process. The OS gives each scheduled task its time slice but the scheduler only contains one task: the infinite for-loop. So who is using the CPU? Only one process, and for 100% because it is the only one scheduled. If you run another program - say the 'DVD2Divx' program - then you will see that the CPU is used for 50% by the infinite for-loop and 50% by the video conversion program. Regards Luciano
