Article Source: http://bbs.chinaunix.net/thread-1613560-1-1.html
Why does an infinite loop occupy high CPU?
If a process is an endless loop, the CPU usage will be very high. However, when the operating system time slice runs, will it automatically switch to another process at a certain time? Even if it is an endless loop, it will still switch to another process at the time. Why is the CPU usage High?
ScutanPublished on
Because other processes are also occupying CPU resources, if other processes sleep or discard the CPU, the scheduler will schedule the dead loop process back for execution.
ConversePublished on
In an endless loop, the program may not be blocked somewhere like other programs, such as sleep and Io .... these programs that may be blocked will not be awakened during the process's sleep and will not occupy the CPU, while the dead loop will not let out the CPU, so it shows a high CPU usage.
Cugb_catPosted on
This type of endless loop is generally calculated at the user layer and is not called.System Call,In addition to the expiration of time slice, the kernel (called by the System) will also cause process scheduling. This is missing from the endless loop program.
ChinesedragonPosted on
The dead loop should not let out the CPU.
ScutanPublished on
The dead loop won't let out the CPU, but the scheduling module of the operating system will schedule all processes as long as the time slice is used up, it will also be placed in the waiting queue of the kernel process.
ProljPublished on
Because the cyclic count also needs to be calculated, and it does not stop being calculated.
Bill2012Posted at, September 18
I don't know which one is correct during study. I also want to know the answer.
ZhoubugPosted on
[Quote] original Post published by [I] cugb_cat [/I] on [url = http://bbs2.chinaunix.net/redirect.php? Goto = findpost & pid = 11470198 & ptid = 1613560] [img] http://bbs2.chinaunix.net/images/common/back.gif?/img=#/url]
This type of endless loop is generally calculated at the user layer and does not call the system call. In addition to the expiration of the time slice, the kernel (called by the System) will also cause process scheduling. This is missing from the endless loop program. [/Quote]
Yes, but the scheduling opportunity is missing.
ZhangxjianPublished on
Wait for a Daniel to parse the details
KouuPublished on
How is the CPU usage calculated? As long as the instance is turned on, the CPU will not rest and the current process will always run on a process.
If the current process is an idle process, even if the CPU idle (because no other process on the CPU is running, the process 0 may be scheduled to run ). Otherwise, even if the CPU is occupied.
It can be seen that there is only one case for the CPU to idle, that is, there is no process in the running queue corresponding to the CPU (that is, there is no process in the running state ).
The endless process is always in the running state. No matter whether the process is being executed or whether it is to be preemptible, no one will change its execution status. So the CPU idle will of course be very low.
According to my understanding, in this case, the CPU usage should always be 100%, but due to some statistical reasons, the CPU usage may occasionally be lower than 100%.
LanguePublished on
You are not familiar with this.Round-Robin.
Most modern operating systems include time slice schedulers. We assume that the CPU has two states: running, idle, and time slice rotation, use a timer to interrupt or in a similar way to notify the operating system to select the next running task after the time slice is completed at a certain time point. Computing-based processes have more opportunities to obtain these time slices. That is to say, if a process keeps making meaningless loops, it will still be scheduled and occupy more time slices, the CPU will remain running for a long time.
ZhangxjianPosted on
A little dizzy. I don't know which one is correct.
System888netPosted on
[Quote] original Post published by [I] zhangxjian [/I] At [url = http://bbs3.chinaunix.net/redirect.php? Goto = findpost & pid = 11488496 & ptid = 1613560] [img] http://bbs3.chinaunix.net/images/common/back.gif?/img=#/url]
A little dizzy. I don't know which one is correct. [/quote]
:) You will not be confused after you have understood the principles and calculation methods of CPU usage.
Net_robberPosted on
Dead loop, nice value a little higher, can also let out the CPU
Geodge831012Published on
To 15th floor
The lower the nice value, the better.
I think the operating system will think that this process is a computing-first process, so the call priority will be very high, although it will also let out the CPU because the time slice is exhausted, however, because it is a computing-first process, the operating system allocates more time.
ZhangxjianPosted on
Several times I mentioned the priority. is the priority of an infinite loop relatively high?
You just followed me.Published on
The 11 th floor is relatively good. If there is no service, more than 90% of the system will be idle. If there is an endless loop, it will not be idle, but will not be interrupted. If you run a test and an endless loop at the same time, it can be seen that the system should take turns scheduling, taking up almost the same time.
HbfnjxPosted on
Other programs are also in use
ZhangxjianPublished
I have understood more than half of them, and I still have some doubts.
ZsmdevPublished on
Adding an endless loop to sleep will make the CPU idle
Haha
PeidrightPublished on
Why is the CPU usage of an infinite loop high?
It is easy to ignore the scheduling factor, because the endless loop is only used for computing.
The question should be: Why is the non-dead cycle low? Because non-endless loops do not require so much computing. A lot of time is spent on Io wait.
Yifeng1118Posted on
First of all, we need to understand how this ratio is calculated. The 11 th floor is very good.
CobrasPosted on
The key is the usage statistics.
CindinxPosted on
[B] Reply [url = http://bbs.chinaunix.net/redirect.php? Goto = findpost & pid = 11469910 & ptid = 1613560] 1 # [/url] [I] zhangxjian [/I] [/B]
If you use the sleep function in the cycle to make the CPU time, it will not occupy that much space.
CobrasPosted on
It seems that I have seen [quote] While (1); [/quote] may affect the interrupt response. It may be caused by the CPU cache.
SparkzhPosted on
[Quote] I have mentioned several priorities. is the priority of an endless loop relatively high?
[Size = 2] [color = #999999] zhangxjian published at 2009-11-21 [/color] [url = http://bbs.chinaunix.net/redirect.php? Goto = findpost & pid = 11501062 & ptid = 1613560] [img] average
Dynamic Priority algorithms increase the priority of endless loops.
SiyikePosted on
[B] Reply [url = http://bbs.chinaunix.net/redirect.php? Goto = findpost & pid = 15597166 & ptid = 1613560] 24 # [/url] [I] cobras [/I] [/B]
The CPU is always occupied by programs. There is no unused time.
Statistics are made during clock interruption.
This statistical program checks the value of the instruction register before the interruption, through which it will know who is running before the interruption.
Common Programs are mostly blocked by calling system calls, such as waiting for keyboard input. At this time, the process is not running, sleep in the kernel.
The endless loop program is in those rows every time it counts. If there is only one infinite loop program, it occupies nearly 100% of the total space.
100% does not mean that the CPU cannot do anything else. If the CPU does other things (for example, the kernel's own Timer Program is started, or another endless loop is started), this ratio will decrease.
Ad8018Published on
[Color = Blue] If the CPU time cannot be exhausted due to endless loops,
Prove that the operating system [color = Red] [size = 7] [B] is not doing well! [/B] [/size] [/color] [/color]
HereareyouPublished on
If a process requests a resource (such as CPU) more frequently than other processes, the system automatically increases the priority of the process to apply for such resources.
Jy02107028Published on
How can I make computing take more time slices? I/O-type processes allocate more time slices and lower nice values?
PeidrightPosted on
As you can understand, the endless loop completely occupies the time slice. In most cases, other processes do not need the complete time slice.
IRPPosted on
[I = s] This post was last edited by IRP at, [/I]
In normal times, most processes are [u] wait in the kernel [/u] and won't be scheduled, or extremely low CPU usage. If the time slice is not used up, yield is finished, the process of an endless loop uses full time slice, And the status is always ready to run, because there is no chance to switch to another State (not scheduled) from the kernel. Two factors: 1. the time slice is full, 2. The probability of being scheduled is the highest. Natural usage comes up naturally.
NOTE: If idle is improperly used, change it to [u] Wait in kernel. [/u] Thanks for your reminder.
XiaonanlnPosted on
The idle process is an endless loop.