Linux——一文彻底了解进程id和线程id的关系(什么是pid、tgid、lwp、pthread_t)

目录

一.内核层面:pid & tgid

二.函数调用层面:getpid & gettid & pthread_self

三.用户层面:PID & LWP(TID)

四.总结


一.内核层面:pid & tgid

首先,我们要清楚,在系统层面只有两个编号:pidtgid

不妨拿出task_struct结构体(进程PCB)看一看:

pid_t pid;  
pid_t tgid;  

我们知道,在linux中进程和线程都使用task_struct结构体实现。

在task_struct中:

pid即线程id。也就是task_struct的编号,每个task_struct各不相同。

tgid叫线程组id。也就是一个进程的id。同一个进程里每一个线程的task_struct中tgid值都一样。
 

二.函数调用层面:getpid & gettid & pthread_self

直接说结论:

getpid获得的是task_struct中的tgid,即线程组编号。

gettid获得的是task_struct中的pid,即线程编号。(也就是LWP/TID

pthread_self获得的是线程在共享区数据的起始地址。

因此,在多线程中,使用getpid获取的全都相同,gettid获取的各不相同。

getpid和gettid是系统接口,获得的是内核数据;而pthread_self是库函数,获得的是用户级数据。

画图表示如下:
ef77d1aea97b4675af153dcbe4ff4167.png

70fec157442b4d42830844d71ba755b3.png

 值得一提的是,编程时gettid不能直接使用,需要自定义封装才行:
93945a3538314705a2959aa66d6af33a.png

pid_t gettid(){
    return static_cast<pid_t>(syscall(SYS_gettid));
}

三.用户层面:PID & LWP(TID)

当使用ps axj或ps -aL查看进程或线程时,显示的PID即线程组编号,也就是task_struct中的tgidLWP即线程编号,也就是task_struct中的pid

可以使用如下代码验证:

#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/syscall.h>
using namespace std;

pid_t gettid(){
    return static_cast<pid_t>(syscall(SYS_gettid));
}

void* func(void* arg){
    cout << pthread_self() << endl;
    cout << "tgid: " << getpid()<< endl;
    cout << "pid: " << gettid() << endl;
    cout << "***********************************************" << endl;
    sleep(5);
}
int main(){
    pthread_t tid[2];
    pthread_create(&tid[0], nullptr, func, nullptr);
    sleep(2);
    pthread_create(&tid[1], nullptr, func, nullptr);
    pthread_join(tid[0], nullptr);
    pthread_join(tid[1], nullptr);
    return 0;
}

结果:

35d553a5410a4635a1bd189de265ae2a.png  

四.总结

含义 内核PCB 系统调用 用户显示(ps命令)
线程编号 pid gettid() LWP/TID
线程组编号 tgid getpid() PID

如有错误,敬请斧正

猜你喜欢

转载自blog.csdn.net/weixin_61857742/article/details/128717592