I'm trying to monitor the action of CPU, and I've found something interesting.

First I have a script named task.sh as below:


for i in {1..999}; do
    sleep 2
    echo $$

Then I execute ./task.sh and at the same time, I execute the command many times ps -Lo psr $(pgrep task) to get the number of CPU, in which the ./task.sh is executing.

I find that the output is changing.

So, does it mean that the CPU will be reassigned to any task which transforms from suspending (sleep) to running? Is this the way that the linux kernel works?

  • In general, when a process yields (gives the OS a chance to schedule other processes in its place), the OS may well end up running it on another CPU next time it's picked from the run queue. A proper answer should explain what CPU affinity is and how to do it on Linux (I'm no Linux guy). – Kusalananda Jan 25 '18 at 8:14
  • @Kusalananda alright, thank you for the tips :) – Yves Jan 25 '18 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.