• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7766
  • Last Modified:

process id and pthread id

Hello there,
    This must be a FAQ. I need to know how can I get the process id of the current pthread.
When I call pthread_self() it returns pthread_t value (integer), which is different from the pid
that I see on "ps" comamnd or gdb.

I need a way to either -

1) get the process id of a (current) pthread


2) An easy way to relate process id to a pthread id from gdb.

  • 2
  • 2
1 Solution
pthread_self() returns the thread id not the process id.  Call getpid() to fetch the id of the current process.

mxjijoAuthor Commented:

>> Call getpid() to fetch the id of the current process
  Yes, getpid() returns the process id. But it is the process id of the process itself.
Take a look at the output of command "ps -ef -L | grep my_proc"

root     17249     1 17249  0   15 May31 ?        00:00:00 my_proc
root     17249     1 17250  0   15 May31 ?        00:00:00 my_proc
root     17249     1 17252  0   15 May31 ?        00:00:00 my_proc
root     17249     1 17253  0   15 May31 ?        00:01:57 my_proc

my_proc has 4 threads in it.

Column 2 (17249) is the process id of the process. getpid() will return 17249, no matter from what thread you call it.
What I need is column 4. Those are the process ids of weight processes (pthreads) running inside the process 17249.

That is the LWP id.  Try calling lwp_from_thread().  [I am not at a Linux box right now, so I don't know if that function is available to user-space apps.
man 2 gettid
mxjijoAuthor Commented:
thanks ravenpl that works.
I cannot call gettid() directly - not compiling but
syscall(NR__gettid) works great.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now