linux下多線程學習4_列印線程id和進程id

來源:互聯網
上載者:User

原文地址::http://hi.baidu.com/neu_stroller/blog/item/4213d8c52ddf0aa18226accb.html

 

 

常式8
    程式目的:實現在建立立的線程中列印該線程的id和進程id
    程式名稱: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;
}

    編譯方法:

gcc -Wall -lpthread pthread_id.c

    執行結果:

Main thread is starting ...
The main process's pid is 3307
New thread ....
This thread's id is 3086347152
The process pid is 3307

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.