|
CS471/571 - Operating Systems
|
Displaying ./code/Pthreads/p1.c
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
// gcc -o p1 p1.c -lpthread
void *thread_main(int *tid)
{
printf("My (real) thread id = %d, process id = %d\n", gettid(), getpid());
printf("My thread id from my parent = %d\n", *tid);
return NULL;
}
int main(void)
{
pthread_t thread_handle1, thread_handle2;
int thread_id1 = 1, thread_id2 = 2;
printf("parent thread id = %d, process id = %d\n", gettid(), getpid());
// Create (like fork then runs thread_main) the thread:
pthread_create(&thread_handle1, NULL, (void *)thread_main, &thread_id1);
pthread_create(&thread_handle2, NULL, (void *)thread_main, &thread_id2);
// Wait for the thread we created to finish:
pthread_join(thread_handle1, NULL);
pthread_join(thread_handle2, NULL);
return 0;
}
|