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

You can use the ps command to query and display the active processor. For example, you might run:

$ ps -aF
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root         1     0  0  5971  1764   1 Sep15 ?        00:00:01 /sbin/init
ubuntu   28903  2975  0  3826  1208   0 09:36 pts/0    00:00:00 ps -aF

The PSR column shows that init is running on processor 1 and ps is running on processor 0. See the manpage for ps(1) for more details on how to customize the fields that are displayed.

You can configure a graphical tool like htop to display the current active processor. Also, htop has a per-CPU load display graph, which may be what you're looking for. See, for example, the following screenshot from http://htop.sourceforge.net/.

htop screenshot

Finally, you can use the taskset tool to force affinity to a particular CPU.


There are many ways to find out. htop, top, ps.

htop

  • tested version: 1.0.2
  • url: http://htop.sourceforge.net/

    1. Hit F2 to get into the setup window
    2. select Columns in the Setup column
    3. go Available Columns
    4. add PROCESSOR htop selecting processor
    5. Check the CPU column htop with cpu column

top

  • tested version: procps 3.3.8
  • url: http://gitorious.org/procps

    1. Hit f to get into the Fields Management window
    2. Select P (Last Used Cpu)

here is an example with the last column P

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND   P 
 5626 yashi     20   0 1926276 545964  47596 R  12.6  3.4 151:10.81 gnome-sh+ 2 
 5347 root      20   0  384788  73600  55708 S   8.7  0.5  55:10.09 Xorg      1 
 8125 yashi     20   0  646240  30776  21928 S   4.3  0.2  23:06.20 gnome-sy+ 0 
 1785 yashi     20   0  581180  29288  15560 R   4.0  0.2   0:25.55 gnome-te+ 1

ps

  • tested version: procps 3.3.8
  • url: http://gitorious.org/procps

PSR is the CODE to display processor id. You can use format option like ps -o pid,psr or simply do ps -eF

$ ps -eF|head
UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root         1     0  0  3817   964   0 Aug14 ?        00:00:02 init [2]  
root         2     0  0     0     0   2 Aug14 ?        00:00:00 [kthreadd]
root         3     2  0     0     0   0 Aug14 ?        00:00:11 [ksoftirqd/0]
root         5     2  0     0     0   0 Aug14 ?        00:00:00 [kworker/0:0H]
root         7     2  0     0     0   0 Aug14 ?        00:00:00 [migration/0]
root         8     2  0     0     0   0 Aug14 ?        00:00:00 [rcu_bh]
root         9     2  0     0     0   3 Aug14 ?        00:00:39 [rcu_sched]
root        10     2  0     0     0   0 Aug14 ?        00:00:00 [watchdog/0]
root        11     2  0     0     0   1 Aug14 ?        00:00:00 [watchdog/1]