in reply to Get PID of child threads

Both top and htop will toggle the display of PID/TID when you press 'H'. The header, somewhat confusingly, remains "PID". In reality, the PID is shared between threads. The ps utility has a separate field for thread ID, called "LWP". In any case, this ID is accessible for the thread via gettid, a Linux-specific syscall...

Here's a snippet that appears to work on my system:

use threads; sub gettid { use Config; die if $Config{archname} !~ /x86_64-linux/; syscall 186 } sub diag { print "pid=$$ tid=@{[threads->tid]} gettid=@{[gettid]}\n"; } async { diag }->join for 1..5;

Edit. s/detach/join/, just in case.

Replies are listed 'Best First'.
Re^2: Get PID of child threads
by TS90 (Novice) on Apr 03, 2017 at 22:29 UTC
    Thanks, that works! The syscall for my OS (arm-linux-gnueabihf-thread-multi-64int) is 224. The following works.
    use threads; sub gettid { syscall 224 } sub diag { print "pid=$$ tid=@{[threads->tid]} gettid=@{[gettid]}\n"; } async { diag }->detach for 1..5;
    pid=16490 tid=1 gettid=16493
    pid=16490 tid=2 gettid=16494
    pid=16490 tid=3 gettid=16495
    pid=16490 tid=4 gettid=16496