The program pstree and htop showing threads with unique PIDS. How is this possible

htopprocesspsthread

I ran the program pstree -p 31872 which printed the following output:

 ruby(31872)─┬─{ruby}(31906)         
             └─{ruby}(32372)

The man page for pstree says:

Child threads of a process are found under the parent process and are
shown with the process name in curly braces, e.g.

       icecast2---13*[{icecast2}]

(The above is displayed differently because of the missing -p option, which disables compaction.)

Running pstree 31872 without -p gives:

ruby───2*[{ruby}] 

When I try to observe those PIDS using ps, no results are found. However, the pids, exist in /proc.

My question is, why would threads have different pids? I would expect them to be the same (31872) as the process. The same behavior is observed when running htop.

Best Answer

The mistake was to presume those numbers were PIDS, when in fact they are TIDS (thread IDs). See Linux function gettid(2). Reading up on clone(2) gives a lot of extra (and interesting) details.