It depends on the threads implementation that you use.
Read pthread_create(3) and pthreads(7). Excerpts:
[...]
In the obsolete LinuxThreads implementation, each of the threads in a
process has a different process ID. This is in violation of the POSIX
threads specification, and is the source of many other nonconformances
to the standard; see pthreads(7).
[...]
LinuxThreads: This is the original Pthreads implementation. Since
glibc 2.4, this implementation is no longer supported.
[...]
The LinuxThreads implementation deviates from the POSIX.1
specification in a number of ways, including the following:
- Calls to getpid(2) return a different value in each thread.
Here is an example showing the concept (built and tun on GNU/Linux
Debian testing)
#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
struct thread_info { /* Used as argument to thread_start() */
pthread_t thread_id; /* ID returned by pthread_create() */
int thread_num; /* Application-defined thread # */
char *argv_string; /* From command-line argument */
};
static int sleep_time = 0;
/* Thread start function: display the thread id and the process id */
static void *
thread_start(void *arg)
{
struct thread_info *tinfo = (struct thread_info *) arg;
printf("thread %lu pid %d\n", tinfo->thread_id, getpid());
if(sleep_time)
{
sleep(sleep_time);
}
return((void*)0);
}
int
main(int argc, char *argv[])
{
int s, tnum, opt, num_threads;
struct thread_info *tinfo;
pthread_attr_t attr;
void *res;
/* The "-s" option specifies a sleep time before the threads destruction */
while ((opt = getopt(argc, argv, "s:")) != -1) {
switch (opt) {
case 's':
sleep_time = strtoul(optarg, NULL, 0);
break;
default:
fprintf(stderr, "Usage: %s [-s sleep_time] arg...\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
num_threads = argc - optind;
/* Initialize thread creation attributes */
s = pthread_attr_init(&attr);
if (s != 0)
handle_error_en(s, "pthread_attr_init");
/* Allocate memory for pthread_create() arguments */
tinfo = calloc(num_threads, sizeof(struct thread_info));
if (tinfo == NULL)
handle_error("calloc");
/* Create one thread for each command-line argument */
for (tnum = 0; tnum < num_threads; tnum++) {
tinfo[tnum].thread_num = tnum + 1;
tinfo[tnum].argv_string = argv[optind + tnum];
/* The pthread_create() call stores the thread ID into
corresponding element of tinfo[] */
s = pthread_create(&tinfo[tnum].thread_id, &attr,
&thread_start, &tinfo[tnum]);
if (s != 0)
handle_error_en(s, "pthread_create");
}
/* Destroy the thread attributes object, since it is no
longer needed */
s = pthread_attr_destroy(&attr);
if (s != 0)
handle_error_en(s, "pthread_attr_destroy");
/* Now join with each thread */
for (tnum = 0; tnum < num_threads; tnum++) {
s = pthread_join(tinfo[tnum].thread_id, &res);
if (s != 0)
handle_error_en(s, "pthread_join");
free(res); /* Free memory allocated by thread */
}
free(tinfo);
exit(EXIT_SUCCESS);
}
Build as follows:
gcc -Wall thpid.c -o thpid -lpthread
Run as follows:
./thpid -s 60 a b c
The output is:
thread 3060976496 pid 8614
thread 3069369200 pid 8614
thread 3077761904 pid 8614
You have one minute to observe with:
ps -e -f -L | grep thpid
peter 8614 2211 8614 0 4 10:36 pts/5 00:00:00 ./thpid -s 60 a b c
peter 8614 2211 8615 0 4 10:36 pts/5 00:00:00 ./thpid -s 60 a b c
peter 8614 2211 8616 0 4 10:36 pts/5 00:00:00 ./thpid -s 60 a b c
peter 8614 2211 8617 0 4 10:36 pts/5 00:00:00 ./thpid -s 60 a b c
|