Get PID from TID

pspthreadsthread

I run iotop to check on programs that are heavy disk users, in case I need to decrease their priority. Usually this is good enough, but iotop only shows thread ID (TID), and sometimes I want to know process ID (PID) so I can find out more about which process is responsible.

Unfortunately, while ps can display TID (a.k.a SPID, LWP), it doesn't have a flag to take a list of TIDs the way it does for a list of PIDs with --pid. The best I can do is list TIDs and then grep the output. For example, if the thread id is 792, I can do

$ ps -eLf | grep ' 792 '

which works reasonably well, but is a little inelegant.

Is there a better way?

Best Answer

You can always do:

ps -eLo pid= -o tid= | awk '$2 == 792 {print $1}'

On Linux:

$ readlink -f /proc/*/task/792/../..
/proc/300

Or with zsh:

$ echo /proc/*/task/792(:h:h:t)
300
Related Question