Search code examples
linuxprocessmonitoringhtop

Why is the status of top or htop always "R" (running)?


top and htop are commonly used tools to monitor processes and computer resources, but why is the status of top itself always R (in the S column of the top)? For example, here is a screenshot of the top command:

top - 12:42:33 up  2:48,  1 user,  load average: 0,11, 0,17, 0,17
Tasks: 319 total,   1 running, 318 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1,1 us,  0,1 sy,  0,0 ni, 98,8 id,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st
MiB Mem :  15968,5 total,   4031,8 free,   2196,1 used,   9740,7 buff/cache
MiB Swap: 122069,0 total, 122069,0 free,      0,0 used.  13325,4 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   2264 xxxxxx    20   0 2784640 320344 184092 S   6,0   2,0   9:14.83 Web Content
   2195 xxxxxx    20   0 3996144 481160 197788 S   2,7   2,9   7:46.13 firefox
   1704 root      20   0  227712 101204  82140 S   1,3   0,6   3:26.54 Xorg
   2993 xxxxxx    20   0  817416  54452  41724 S   1,0   0,3   0:04.27 gnome-terminal-
   1856 xxxxxx    20   0 4521152 385832 123440 S   0,7   2,4   2:39.79 gnome-shell
   1202 root     -51   0       0      0      0 S   0,3   0,0   0:52.07 irq/44-nvidia
   5048 xxxxxx    20   0   11980   3888   3132 R   0,3   0,0   0:00.12 top
      1 root      20   0  167972  11864   8480 S   0,0   0,1   0:03.91 systemd
      2 root      20   0       0      0      0 S   0,0   0,0   0:00.00 kthreadd
      3 root       0 -20       0      0      0 I   0,0   0,0   0:00.00 rcu_gp
      4 root       0 -20       0      0      0 I   0,0   0,0   0:00.00 rcu_par_gp
      6 root       0 -20       0      0      0 I   0,0   0,0   0:00.00 kworker/0:0H-kblockd
      9 root       0 -20       0      0      0 I   0,0   0,0   0:00.00 mm_percpu_wq
     10 root      20   0       0      0      0 S   0,0   0,0   0:00.09 ksoftirqd/0
     11 root      20   0       0      0      0 I   0,0   0,0   0:01.49 rcu_sched
     12 root      rt   0       0      0      0 S   0,0   0,0   0:00.02 migration/0
     13 root     -51   0       0      0      0 S   0,0   0,0   0:00.00 idle_inject/0

From the source code of top (procps/top), it gets process statuses from the file /proc/<pid>/stat, while the status of top, for most of the time, is S (sleeping) if it is continuously printed with the following command:

watch -n.1 "cat /proc/<top-pid>/stat | grep -o \"[S|R]\""

Solution

  • Well... if you are running top (or htop), and top (or htop) gets its own status from /proc/self/stat... then it must be running, otherwise how would it be possible that it got its status while not running? The sole fact that a process is reading its own status means that the status must be running at the time of reading.

    You can try this with other programs too, if you want:

    $ cat /proc/self/stat
    32482 (cat) R ...
    $ head /proc/self/stat
    32491 (head) R ...
    $ tail /proc/self/stat
    32497 (tail) R ...
    $ less /proc/self/stat
    32503 (less) R ...