Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do two identical processes don't take the double CPU load

Tags:

process

I am using a Raspberry Pi 3 and basically stepped over a little tripwire.

I have a very big and complicated program that takes away a lot of memory and has a big CPU load. I thought it was normal that if I started the same process while the first one was still running, it would take the same amount of memory and especially double the CPU Load. I found out that it doesn't take away more memory and does not affect the CPU load.

To find out if this behavior came from my program, I wrote a tiny c++ program that has extremely high memory usage, here it is:

#include <iostream>

using namespace std;

int main()
{
    for(int i = 0; i<100; i++) {

        float a[100][100][100];
        for (int i2 = 0; i2 < 99; ++i2) {
            for (int i3 = 0; i3 < 99; ++i3){
                for (int i4 = 0; i4 < 99; ++i4){
                    a[i2][i3][i4] = i2*i3*i4;
                    cout << a[i2][i3][i4] << endl;
                }
            }
        }
    }
    return 0;
}

The CPU-load is about at 30 % of the max-level, I started the code in one terminal. Strangely, when I started it in another terminal at the same time, it didnt affect the CPU load. I concluded that this behaviour couldn't come from my program.

Now I want to know:

  • Is there a "lock" that ensures that a certain type of process does not grill your cores?

  • Why don't two identical processes double the CPU load?

like image 238
monamona Avatar asked Dec 14 '25 23:12

monamona


1 Answers

Well, I found out that there is a "lock" that makes sure a process doesn't take away all memory and makes the CPU load go up to 100%. It seems that than more processes there are, than more CPU load is there, but not in a linear way.

Additionally, the code I wrote to look for the behaviour has only high memory usage, the 30% level came from the cout in the standard library . Multiple processes can use the command at the same time without increasing the CPU load, but it affects the speed of the printing.

When I found out that, I got suspicious about the programs speed. I used the analytics in my IDE for c++ to find out the duration of my original program, and indeed it was a bit more that two times slower.

That seems to be the solution I was looking for, but I think this is not really applicable to a large audience since the structure of the Raspberry Pi is very particular. I don't know how this works for other systems.

BTW: I could have guessed that there is a lock. I mean, if you start 10 processes that take away 15% of the CPU load max level, you would have 150% CPU usage. IMPOSSIBLE!

like image 85
monamona Avatar answered Dec 16 '25 23:12

monamona



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!