Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

Thanks!

share|improve this question
up vote 2 down vote accepted

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.

share|improve this answer
    
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .