Quantcast
Channel: GoLinuxHub
Viewing all articles
Browse latest Browse all 392

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

$
0
0
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

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


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


Follow the below links for more tutorials

How to find the path of any command in Linux
How to configure a Clustered Samba share using ctdb in Red Hat Cluster
How to delete an iscsi-target from openfiler and Linux
How to perform a local ssh port forwarding in Linux
How to use yum locally without internet connection using cache?
What is umask and how to change the default value permanently?
Understanding Partition Scheme MBR vs GPT
How does a successful or failed login process works in Linux
How to find all the process accessing a file in Linux
How to exclude multiple directories from du command in Linux
How to configure autofs in Linux and what are its advantages?
How to resize software raid partition in Linux
How to configure Software RAID 1 mirroring in Linux
How to prevent a command from getting stored in history in Linux


Viewing all articles
Browse latest Browse all 392

Trending Articles