linux 概念之pid tid区分

来源:互联网 发布:js copy对象 编辑:程序博客网 时间:2024/06/06 05:52

以前对linux下的线程id,进程id,真实线程id理解不够。自己记录一下。
linux下查看pid和tid的命令:

进程pid: getpid()
线程tid: pthread_self() //进程内唯一,但是在不同进程则不唯一。
线程pid: syscall(SYS_gettid) //系统内是唯一的

不同进程中创建线程,可能线程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;}

参考:http://blog.csdn.net/u012398613/article/details/52183708

0 0
原创粉丝点击