How to find out on which core a process is running on in Linux

How to find out on which core a process is running on in Linux

There are many processes in Linux which run on single core while many will switch between different core depending upon the availability.

With below two methods you can find if a process is continuously running on single core or if it is switching between all the available cores based on the availability.

Method 1

Below command can be used

# ps -o pid,psr,time,comm -p $(pgrep -x fio)
  PID PSR     TIME COMMAND
 8453   4 00:00:00 fio
 8455   8 00:00:01 fio

Here 'psr' column shows the last used core by the process. I have used pgrep to grep the pid of the process running by the name of 'fio'. Everytime I hit the same command I observe the processor to be changing

# ps -o pid,psr,time,comm -p $(pgrep -x fio)
  PID PSR     TIME COMMAND
 8453   4 00:00:00 fio
 8455   8 00:00:03 fio

# ps -o pid,psr,time,comm -p $(pgrep -x fio)
  PID PSR     TIME COMMAND
 8453   9 00:00:00 fio
 8455   8 00:00:03 fio

So to monitor this for a while I can run it in a while loop, here I have used a sleep of 5 seconds which you can increase based on the requirement

# while true; do echo -ne "`ps -o pid,psr,time,comm -p $(pgrep -x fio) `t"; date;sleep 5;done
  PID PSR     TIME COMMAND
 8453   4 00:00:00 fio
 8455   9 00:00:07 fio  Fri Jul  7 18:47:13 IST 2017
  PID PSR     TIME COMMAND
 8453   4 00:00:00 fio
 8455   9 00:00:07 fio  Fri Jul  7 18:47:18 IST 2017
  PID PSR     TIME COMMAND
 8453   4 00:00:00 fio
 8455   9 00:00:07 fio  Fri Jul  7 18:47:23 IST 2017
  PID PSR     TIME COMMAND
 8453   4 00:00:00 fio
 8455   9 00:00:07 fio  Fri Jul  7 18:47:28 IST 2017
  PID PSR     TIME COMMAND
 8453   4 00:00:00 fio
 8455   9 00:00:07 fio  Fri Jul  7 18:47:33 IST 2017
  PID PSR     TIME COMMAND
 8453   5 00:00:00 fio
 8455   9 00:00:07 fio  Fri Jul  7 18:47:38 IST 2017

Method 2

'top' is a nice tool which can also give you this information.

Execute 'top' from your terminal

Press "f" to enter the field menu as below
How to find out on which core a process is running on in Linux
Press "J" as the menu shows which will display the last used CPU
Hit "Enter"

Now you should see a new column in your top command output
How to find out on which core a process is running on in Linux

I hope the article was useful.

Related Articles:
Understanding Load Average in Linux and when to be worried about it?
How to run a process in background or bring to foreground in Linux
10 examples to help you understand top command usage in Unix/Linux

2 thoughts on “How to find out on which core a process is running on in Linux”

Leave a Comment