You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: Linux is the only platform allowing to instantiate a thread via Process class as in thread = Process(tid), and query the thread as if it was a process.
In this case p2.ppid() (name) is bash instead of python (which is "us", the process who actually started the thread).
Apparently, when Linux deals with a process thread, /proc/{TID}/status behaves differently in terms of Tgid and PPid columns. E.g. this is a normal process:
When Pid and Tgid are different, that indicates we're dealing with a thread (this I knew already). The problem is that PPid does not point to the process which spawned the thread ("python"in my case) but its parent ("sh"):
giampaolo
changed the title
[Linux] Process.ppid() is incorrect in case of Process thread
[Linux] ppid() is incorrect in case of Process thread
Feb 27, 2025
Note: Linux is the only platform allowing to instantiate a thread via Process class as in
thread = Process(tid)
, and query the thread as if it was a process.This test case fails:
In this case
p2.ppid()
(name) isbash
instead ofpython
(which is "us", the process who actually started the thread).Apparently, when Linux deals with a process thread,
/proc/{TID}/status
behaves differently in terms ofTgid
andPPid
columns. E.g. this is a normal process:And this is a thread:
When
Pid
andTgid
are different, that indicates we're dealing with a thread (this I knew already). The problem is thatPPid
does not point to the process which spawned the thread ("python"in my case) but its parent ("sh"):This exact condition is also handled by forkstat util:
https://github.com/ColinIanKing/forkstat/blob/6d3bea2af4d7fa923b4647feb40244f154f9f5ad/forkstat.c#L700-L733
When dealing with a thread, it takes the ppid from
Tgid
column instead ofPPid
. This seems to return the correct result.The text was updated successfully, but these errors were encountered: