Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Tuesday, July 17, 2012

Find out threads of a process in Linux

different methods to find number of threads in a process in linux.

[onega@localhost ~]$ ps aux | grep eclipse
onega     3144  4.8 14.4 790568 187504 ?       Sl   04:32   0:47 /opt/eclipse/eclipse
onega     3471  0.0  0.0   4016   668 pts/4    R+   04:48   0:00 grep eclipse
[onega@localhost ~]$ ls /proc/3144/task
3144  3145  3146  3147  3148  3149  3150  3151  3154  3155  3156  3157  3159  3160  3174  3178  3182  3183  3250  3288  3291  3348  3353
[onega@localhost ~]$ ls /etc/*release
/etc/redhat-release
[onega@localhost ~]$ cat /etc/redhat-release
CentOS release 5.6 (Final)
[onega@localhost ~]$ ps -eLf | grep eclipse
[onega@localhost ~]$ ps -Lf 3144

the thread ID under /proc/3144/task matches value of the following code:
pid_t system_thread_id = (pid_t)syscall(SYS_gettid);
Other functions seems to return thread ID, but their return value is different from values under task/. E.g.
ACE_Thread::self();
ACE_Thread_Manager::thr_self();
ACE_OS::thr_self();
boost::thread::id();
boost::this_thread::get_id();
pthread_self();