I have the pid of the process. I would like to know the current number of thread running in the process. I know I can read /proc/pid/stat for this information, but I'm not sure how to specifically get the number of threads from stat. Can anyone help?



The number of entries in /proc/pid/task is the number of threads in the process. Also, /proc/pid/status has a Threads line. In /proc/pid/stat, it's the 20th field.

  • Ah that a lot I would try this but I just realized my method to get the process id is incorrect. I'm running user space code that read and writes to a proc file. I use a module to do the read and writes to a proc file. Within the module code, can I get the process id of the process that tries to write to a proc file? Thanks for any help! – Irlanco Sep 29 '12 at 20:57
  • If you want to get your own information, just use /proc/self, it's a shortcut for getting your own PID and composing a path containing it. Of course, you can also call getpid to get your own PID. – David Schwartz Sep 29 '12 at 20:58
  • I believe getpid() from a user-space library and does not work in kernel space. Is this true? Or is there a kernel version? Otherwise I will try the /proc/self thanks! – Irlanco Sep 29 '12 at 21:00
  • If you want to do it from kernel code, look at how proc does it and copy that. You need a task_struct and you need to atomically read its signal->count member. To access the current task, use current. – David Schwartz Sep 29 '12 at 21:11

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.