Edgar Sampere
Edgar Sampere

Reputation: 273

Which Linux syscall is used to get a thread's ID?

I have to implement a wrapper function that serves as pthread_self() to get a pthread ID but I've been searching and haven´t found which syscall does this. Reading another post from Stack O. I know clone() is used to create threads, also that I can trace the syscalls with ptrace() but before tracing it by hand...could someone knows which syscall is?

Upvotes: 3

Views: 5652

Answers (4)

Ramanand Yadav
Ramanand Yadav

Reputation: 329

This might be helpfull.

UINT32 tid= syscall(SYS_gettid);

Upvotes: 0

J Jorgenson
J Jorgenson

Reputation: 1499

There are 3 different IDs for a linux process thread: pid, pthread id, and tid.

The 'pid' is global and equivalent to the parent process id, and is easily obtained by 'getpid()'. This value is unique, but only for the duration of an the active process assigned the given id. This value may be 'recycled' for a new process after a process is terminated and new ones are spawned. This value is the same across all threads, within a process. This value is what you'll see in top, and htop, 'ps -ef', and pidstat.

The 'pthread id' is reported by pthread_create() and phtread_self(). This is value is unique only within the process, and only for the duration of the assign thread. This value may be 'recycled' as threads are terminated and spawned. This value is not unique across the system, nor across threads that have been terminated and started. This value is NOT visible outside of a program. This value is opaque and may be a pointer or structure depending on the platform.

The 'tid' Thread id is reported by gettid(). This was introduced to Linux 2.4, and does not appear to be available on other platforms. This value is unique within the process and across the system. This value is reported by top and htop, and 'pidstat -t'. I'm not 100% certain, but suspect this value can be 'recycled' as processes are terminated and spawned. This is the value that appears in the Linux tools 'top','htop', 'pidstat -t', and 'ps -efL', when shown threads.

Documentation for gettid: linux.die.net/gettid

You can obtain 'gettid()' through:

#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>

My CentOS 6.5 is not properly setup and missing the gettid prototype, though the documentation says it should be present through the above #includes. Here is a macro that mimics 'gettid':

#ifndef gettid
// equivalent to:  pid_t  gettid(void)
#define gettid() syscall(SYS_gettid)
#endif

Be aware that since this is a syscall(), you'll gain efficiency by caching the result and avoiding repeatedly using the syscall().

Upvotes: 3

chill
chill

Reputation: 16898

In glibc, pthread_self() does not do system calls, but returns a pointer to a struct pthread, located in the TSD segment.

Upvotes: 1

jmiserez
jmiserez

Reputation: 3109

How about syscall 0xe0, gettid()?

gettid() returns the caller's thread ID (TID). In a single-threaded process, the thread ID is equal to the process ID (PID, as returned by getpid(2)). In a multithreaded process, all threads have the same PID, but each one has a unique TID. For further details, see the discussion of CLONE_THREAD in clone(2).

Upvotes: 2

Related Questions