简体   繁体   中英

Regarding CPU utilization

Considering the below piece of C code, I expected the CPU utilization to go up to 100% as the processor would try to complete the job (endless in this case) given to it. On running the executable for 5 mins, I found the CPU to go up to a max. of 48%. I am running Mac OS X 10.5.8; processor: Intel Core 2 Duo; Compiler: GCC 4.1.

int i = 10;
while(1) {
    i = i * 5;
}

Could someone please explain why the CPU usage does not go up to 100%? Does the OS limit the CPU from reaching 100%?

Please note that if I added a "printf()" inside the loop the CPU hits 88%. I understand that in this case, the processor also has to write to the standard output stream hence the sharp rise in usage.

Has this got something to do with the amount of job assigned to the processor per unit time?

Regards, Ven.

您拥有多核处理器,并且处于单线程方案中,因此您将仅使用一个核全油门...为什么您希望在类似情况下整体处理器使用率达到100%?

Run two copies of your program at the same time. These will use both cores of your "Core 2 Duo" CPU and overall CPU usage will go to 100%


Edit

if I added a "printf()" inside the loop the CPU hits 88%.

The printf send some characters to the terminal/screen. Sending information, Display and Update is handeled by code outside your exe, this is likely to be executed on another thread. But displaying a few characters does not need 100% of such a thread. That is why you see 100% for Core 1 and 76% for Core 2 which results in the overal CPU usage of 88% what you see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM