user4828019
user4828019

Reputation:

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.

Upvotes: 0

Views: 78

Answers (2)

DrKoch
DrKoch

Reputation: 9772

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.

Upvotes: 0

aleroot
aleroot

Reputation: 72636

You have a multicore processor and you are in a single thread scenario, so you will use only one core full throttle ... Why do you expect the overall processor use go to 100% in a similar context ?

Upvotes: 2

Related Questions