How do cgroups interact with non-cgrouped processes

cgroups

I have 3 processes on a single core machine. Each process is exactly the same and burns CPU as fast as it can (tight loop). Two of them (A and B) are launched with cgexec in separate groups with shares set to 512 and 512. The third (C) is launched regularly, not with cgexec.

Once all are up and running, C gets 66% of the CPU while A and B split 33%. If I kill C then A and B get 50% each (as expected).

Why does C get 66%? I would have expected 33% each in this scenario, or maybe 50% (C), 25% (A) and 25% (B). 66% going to C doesn't work out no matter how I do the math though.

In general, I want to understand how processes launched without cgexec interact with processes launched with cgexec when it comes to resource sharing (CPU in particular, but a more general answer would be appreciated if it isn't too complex).

Best Answer

Cgroups are hierarhical and they are inherited by all subprocesses. So all processes must be in some cgroup. By default it's the root cgroup and by default it has 1024 shares which is twice as A and B in Your example.

CPU time is shared betwean cgroups according to weight assigned to them in cpu.shares.

If A had 1024 shares and B 512 and C 256 and D 256, the cpu time distribution whould be A - 50%, B - 25%, C and D 12,5%.

Related Question