Original address:: http://hi.baidu.com/neu_stroller/blog/item/4213d8c52ddf0aa18226accb.html
Routine 8
Program Purpose: To print the ID and process ID of the thread in a newly created thread
Program Name: PTHREAD_ID.C
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>/*getpid () * *
void *create (void *arg)
{
printf ("New thread ... \ n");
printf ("This thread ' s ID is%u \ n", (unsigned int) pthread_self ());
printf ("The process PID is%d \ n", Getpid ());
return (void *) 0;
}
int main (int argc,char *argv[])
{
pthread_t Tid;
int error;
printf ("Main thread is starting ... \ n");
Error = pthread_create (&tid, NULL, create, NULL);
if (Error)
{
printf ("Thread is not created ... \ n");
return-1;
}
printf ("The main process ' s PID is%d \ n", Getpid ());
Sleep (1);
return 0;
}
Compilation method:
Gcc-wall-lpthread PTHREAD_ID.C
Execution results:
Main thread is starting ...
The main process ' s PID is 3307
New thread ....
This thread ' s ID is 3086347152
The process PID is 3307