Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Get PID of child threads

by oiskuu (Hermit)
on Apr 03, 2017 at 22:15 UTC ( [id://1186913]=note: print w/replies, xml ) Need Help??


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
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1186913]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-29 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found