多线程中的pid

由一个进程创建的多个线程具有相同的 pid,也就是在这些线程中分别调用 getpid(),得到的结果是一样的。
这里的 pid 其实是 task struct(进程描述符结构体)中的 tgid,它是线程的 group ID,也是主线程(它创建了子线程)的 PID。
而 task struct 中的 pid 其实是线程的 ID, 也可以叫作 tid(thread ID),对于多线程而言,这些 tid 是不同的。
借用 stackoverflow 上的一个回答来解释这里面的逻辑,下面这张图形象的描述了多进程和多线程中 pid 与 tgid 的关系。

最后引用内核中 kernel/sys.c 文件里关于 getpid() 系统调用的注释来说明这个问题: 

/**                     
 * sys_getpid - return the thread group id of the current process
 *                      
 * Note, despite the name, this returns the tgid not the pid.  The tgid and
 * the pid are identical unless CLONE_THREAD was specified on clone() in
 * which case the tgid is the same in all threads of the same group.
 *                      
 * This is SMP safe as current->tgid does not change.
 */                     
SYSCALL_DEFINE0(getpid)
{                       
    return task_tgid_vnr(current);                                                                                                                                                                                                            
}                       




参考资料:
1、https://stackoverflow.com/questions/9305992/if-threads-share-the-same-pid-how-can-they-be-identified

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值