6

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?

7

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
  • 1
    But what if there are 2770 tasks! – Irfan Latif Jul 3 '19 at 3:49
  • @IrfanLatif Could you elaborate? – Nathaniel M. Beaver Sep 8 '19 at 12:45
  • @NathanielM.Beaver I use cgroups on Android to control CPU/RAM usage, internet connectivity etc. Cgroups go for TIDs, not PIDs. Two TIDs belonging to same PID can be in different cgroups. So I have to deal with thousands of TIDs. Loops, pipes make it slow. So I was looking for some native code solution, not a programmer myself. – Irfan Latif Sep 8 '19 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.