1

when i do a ps -efT (where -T = Show threads, possibly with SPID column.), i see all the threads have the same PID, which is as expected.

myroot   24958 24958  7942  0 20:20 pts/12   00:00:00 java -jar myapp.jar
myroot   24958 24959  7942  0 20:20 pts/12   00:00:11 java -jar myapp.jar
myroot   24958 24960  7942  0 20:20 pts/12   00:00:00 java -jar myapp.jar
myroot   24958 24961  7942  0 20:20 pts/12   00:00:00 java -jar myapp.jar
myroot   24958 24962  7942  0 20:20 pts/12   00:00:00 java -jar myapp.jar
myroot   24958 24963  7942  0 20:20 pts/12   00:00:00 java -jar myapp.jar
myroot   24958 24964  7942  0 20:20 pts/12   00:00:00 java -jar myapp.jar
myroot   24958 24965  7942  0 20:20 pts/12   00:00:00 java -jar myapp.jar

As it can be seen above, all the threads share/show the same PID 24958.


Now When I do the same with top or htop, i am seeing differnt pid for each thread and this is bothering me. Is there a way to show the same PID for all the threads. Below is the curtailed output for top -H -p 24958 (I am using top with -p, so i could explain and show the problem)

top - 21:42:44 up 9 days, 18:38,  0 users,  load average: 0.00, 0.26, 0.82
Threads:  32 total,   0 running,  32 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :  12542.5 total,  10135.3 free,    826.8 used,   1580.4 buff/cache
MiB Swap:   4096.0 total,   4096.0 free,      0.0 used.  11439.4 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
24958 myroot    20   0 7036228 340720  21084 S   0.0   2.7   0:00.00 java
24959 myroot    20   0 7036228 340720  21084 S   0.0   2.7   0:11.99 java
24960 myroot    20   0 7036228 340720  21084 S   0.0   2.7   0:00.43 GC Thread#0
24961 myroot    20   0 7036228 340720  21084 S   0.0   2.7   0:00.00 G1 Main Marker
24962 myroot    20   0 7036228 340720  21084 S   0.0   2.7   0:00.00 G1 Conc#0
--and few more threads.    

When i use top -H, i would not have any means to say which all threads belong to same Process unless I see same PID for all of them.
Any guidence on how to get the same PID for all the threads when using top (or htop. As I have observed, htop too has the same issue).

Given @user1686 answer to use TGID column. I am wondering what the PID for the thread is refering to.

1 Answer 1

5

With htop, you want the TGID column (add it through F2 > Columns). It is also available in top with the same name, but I don't know how to configure top.

Linux "processes" are really just thread groups (or task groups), and the "PID" column in top/htop actually shows the thread ID (task ID). The same clone(2) system call is used to create both – check out the part about CLONE_THREAD.

2
  • that worked even in top ++1. shift+f takes you there in top. Could you confirm - 1) then what is the PID value actually refering to for these threads. 2) if a process has more than one Thread Groups with in it, then would they all share the same TGID. Is it right? Coz in java i can create Thread Groups ( with some name ) from the program but there seems to be no way to assign or get the Thread Group Id.
    – samshers
    Commented Dec 28, 2021 at 19:17
  • 1
    No, a process is a thread group. It can't have more than one; then you would have two processes. The thread groups that you create in Java have nothing to do with thread groups in Linux – only the name is propagated to all grouped threads (and you can see it in htop), but the grouping itself remains internal to the JRE. Commented Dec 28, 2021 at 19:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .