Linux Process CPU – How to Determine the Processor Running a Process

cpulinuxprocesstop

I have access to an 8-core node of a Linux cluster. When logged in to the node, I can see a list of processors using this command:

more /proc/cpuinfo

In my 8-core node, the processors are numbered from 0 to 7. Each processor is an Intel Xeon CPU (E5430 @ 2.66GHz).

Now suppose I call the program foo with some arguments args:

foo args

The program foo takes a long time to execute (hours or days, for example). Having called foo, is it possible to determine the particular processor (i.e., 0 to 7) on which foo is running? The top program shows me the process ID and similar information, but I don't see the processor number. Is such information available?

Best Answer

ps can give you that information if you ask for the psr column (or use the -F flag which includes it).

Ex:

$ ps -F $$
UID        PID  PPID  C    SZ   RSS PSR STIME TTY      STAT   TIME CMD
me        6415  6413  0  5210  2624   2 18:52 pts/0    SN     0:00 -su

Or:

$ ps -o pid,psr,comm -p $$
  PID PSR COMMAND
 6415   0 bash

My shell was running on CPU 2 when I ran the first command, on CPU 0 when I ran the second. Beware that processes can change CPUs very, very quickly so the information you actually see is, essentially, already stale.

Some more info in this Super User question's answers:

Linux: command to know the processor number in which a process is loaded?

Related Question