11

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?

Nathaniel M. Beaver
  • 1,246
  • 13
  • 28

1 Answers1

11

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
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • 1
    But what if there are 2770 tasks! – Irfan Latif Jul 03 '19 at 03:49
  • @IrfanLatif Could you elaborate? – Nathaniel M. Beaver Sep 08 '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 08 '19 at 13:31