Linux under Multithreaded learning 4_ print thread IDs and processes Id__linux

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.