Monday, September 29, 2008

Identifying threads under a given process

This is quick note. The method to identify all the threads under a given process ID differs significantly between Linux kernel versions 2.4.x and 2.6.x.

In case of 2.4.x kernel, you should look for process IDs that are prefixed with dot (".") under the /proc folder. All these PIDs correspond to threads. If you open the /proc/.X/status file , the number appearing against "Tgid:" is the PID of the process that this thread corresponds to. So there is no direct way to tell the list of all the threads undre a given PID. The only way you can get that list is to iterate through all the directories that begin with dot under /proc, and check the "Tgid:" to be matching the PID in question.

In case of 2.6.x kernel, all you need to do is just to get the list of all the directories under /proc/PID/task. Each one of these IDs will correspond to one thread of the process. Also you will exactly know how many threads are there by checking the "Threads:" value in the /proc/PID/status file.

Check if your ps supports the option "-L". If it is supported, you can use that to get better information about the threads under each process.

No comments: