Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

up vote 0 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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