Linux下线程pid和tid

#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;
}

Linux下线程pid和tid

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

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

Linux下线程pid和tid

上一篇:android如何添加数组资源和菜单资源?


下一篇:JavaScript权威指南第13章 web浏览器中的javascript