码迷,mamicode.com
首页 > 编程语言 > 详细

Linux下线程pid和tid

时间:2014-06-15 09:40:46      阅读:526      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   color   

#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>

struct message
{
    int i;
    int j;
};

void *hello(struct message *str)
{
    printf("child, the tid=%lu, pid=%d\n",pthread_self(),syscall(SYS_gettid));
    printf("the arg.i is %d, arg.j is %d\n",str->i,str->j);
    printf("child, getpid()=%d\n",getpid());
    while(1);
}

int main(int argc, char *argv[])
{
    struct message test;
    pthread_t thread_id;
    test.i=10;
    test.j=20;
    pthread_create(&thread_id,NULL,hello,&test);
    printf("parent, the tid=%lu, pid=%d\n",pthread_self(),syscall(SYS_gettid));
    printf("parent, getpid()=%d\n",getpid());
    pthread_join(thread_id,NULL);
    return 0;
}

bubuko.com,布布扣

getpid()得到的是进程的pid,要得到线程的pid,必须用syscall(SYS_gettid);

Linux下线程pid和tid,布布扣,bubuko.com

Linux下线程pid和tid

标签:style   class   blog   code   http   color   

原文地址:http://www.cnblogs.com/lakeone/p/3789117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!