Skip to content
Advertisement

Changing nice value of a process has no effect in Linux

I read about APUE 3rd, 8.16 Process Scheduling, there is an example written to verify that changing nice value of a process will affect its priority, I rewrite the code like below:

JavaScript

And the result of the example is shown below:
NZERO = 20
parent nice:20
child nice:20, adjusted by 0
child now nice:20
parent count:601089419
child count:603271014
Looks like no effect has been made on the child process, why? And how to make the result the way I expect?
(my platform is: Linux liucong-dell 4.4.0-93-generic #116~14.04.1-Ubuntu SMP Mon Aug 14 16:07:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux)

Advertisement

Answer

Your multi-core CPU can happily run both the parent and the child simultaneously. As long as both can run, their relative priorities don’t matter.

In order to see the effect of nice, you have to load your machine such that there’s always a process ready and waiting to run. The easiest way would be making your test multithreaded. Spawn (after the fork) a few worker threads in both the parend and the child, make them all increment the counter (make it either atomic or thread local), and see what happens.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement