long tid = syscall(SYS_gettid); printf("%ld\n", tid); So the entire code with this is would be:
#include <pthread.h> #include <stdio.h> #include <unistd.h> #include <syscall.h> void* thread_function (void* arg) { long tid = syscall(SYS_gettid); printf("child thread TID is %ld\n", tid); fprintf (stderr, "child thread pid is %d\n", (int) getpid ()); /* Spin forever. */ while (1); return NULL; } int main () { pthread_t thread; long tid = syscall(SYS_gettid); printf("main TID is %ld\n", tid); fprintf (stderr, "main thread pid is %d\n", (int) getpid ()); pthread_create (&thread, NULL, &thread_function, NULL); /* Spin forever. */ while (1); return 0; } Giving an example output of:
main TID is 17963 main thread pid is 17963 thread TID is 17964 child thread pid is 17963