Skip to content

Instantly share code, notes, and snippets.

@ducalpha
Created January 15, 2014 00:55
Show Gist options
  • Save ducalpha/8428910 to your computer and use it in GitHub Desktop.
Save ducalpha/8428910 to your computer and use it in GitHub Desktop.
demonstrate the difference between user space PID, TID, and kernel space light-weight process (LWP) id
all:
gcc pid_tid.c -lpthread
./a.out
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <syscall.h>
#include <unistd.h>
void *pr_pid_tid(void *arg) {
pid_t my_pid = getpid();
printf("User space PID is %d\n", my_pid);
pthread_t my_tid = pthread_self();
printf("User space TID is %ld\n", my_tid);
int my_sid = syscall(SYS_gettid);
printf("Kernel space LWP ID is %d\n", my_sid);
return NULL;
}
int main() {
pthread_t new_thread_tid = 0;
printf("Child thread:\n");
pthread_create(&new_thread_tid, NULL, pr_pid_tid, NULL);
pthread_join(new_thread_tid, NULL);
printf("New thread id is %ld\n", new_thread_tid);
printf("Parent thread:\n");
pr_pid_tid(NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment