Skip to main content
Post Undeleted by terdon
Post Deleted by CommunityBot
Added full code lsiting
Source Link
user123570
user123570
 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 
long tid = syscall(SYS_gettid); printf("%ld\n", tid); 
 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 
added 122 characters in body
Source Link
user123570
user123570

(Userspace) threads are not implemented as processes as such on Linux, in that that they do not have their own private address space, they still share the address space of the parent process.

However, these threads are implemented to use the kernel process accounting system, so are allocated their own PIDThread ID (TID), but are given the same PID and 'thread group ID' (TGID) as the parent process - this is in contrast to a fork, where a new TGID and PID are created, and the TID is the same as the PID.

This way all scheduling / process resources implemented in the kernelSo it appears that recent kernels had a separate TID that can be applied toqueried, it is this that is different for threads as well as processes., a suitable code snippet to show this in each of the main() thread_function() above is:

long tid = syscall(SYS_gettid); printf("%ld\n", tid); 

(Userspace) threads are not implemented as processes as such on Linux, in that that they do not have their own private address space, they still share the address space of the parent process.

However, these threads are implemented to use the kernel process accounting system, so are allocated their own PID, but are given the same 'thread group ID' (TGID) as the parent process - this is in contrast to a fork, where a new TGID and PID are created.

This way all scheduling / process resources implemented in the kernel can be applied to threads as well as processes.

(Userspace) threads are not implemented as processes as such on Linux, in that that they do not have their own private address space, they still share the address space of the parent process.

However, these threads are implemented to use the kernel process accounting system, so are allocated their own Thread ID (TID), but are given the same PID and 'thread group ID' (TGID) as the parent process - this is in contrast to a fork, where a new TGID and PID are created, and the TID is the same as the PID.

So it appears that recent kernels had a separate TID that can be queried, it is this that is different for threads, a suitable code snippet to show this in each of the main() thread_function() above is:

long tid = syscall(SYS_gettid); printf("%ld\n", tid); 
Source Link
user123570
user123570

(Userspace) threads are not implemented as processes as such on Linux, in that that they do not have their own private address space, they still share the address space of the parent process.

However, these threads are implemented to use the kernel process accounting system, so are allocated their own PID, but are given the same 'thread group ID' (TGID) as the parent process - this is in contrast to a fork, where a new TGID and PID are created.

This way all scheduling / process resources implemented in the kernel can be applied to threads as well as processes.