C语言__TID__用法,C语言编程中pid, tid以及真实pid的关系

本文介绍了C语言编程中进程和线程的标识符pid, tid以及它们与真实pid的关系。通过示例代码展示了如何获取这些标识符,并解释了在Linux系统中pid和tid的用途,包括查看pid和tid的方法。" 107485790,9551830,Linux实战:C语言实现简易cp指令,"['C语言', 'Linux']
摘要由CSDN通过智能技术生成

对于ubuntu14.04操作系统,可以在/usr/src/linux-headers-4.4.0-31/include/linux/sched.h文件中看到进程控制块的结构体,如下

structtask_struct {volatile long state; /*-1 unrunnable, 0 runnable, >0 stopped*/

void *stack;

atomic_t usage;

unsignedint flags; /*per process flags, defined below*/unsignedintptrace;

#ifdef CONFIG_SMPstructllist_node wake_entry;inton_cpu;

unsignedintwakee_flips;

unsignedlongwakee_flip_decay_ts;struct task_struct *last_wakee;intwake_cpu;#endif......

......

pid_t pid;

pid_t tgid;

......

......

}

可以看到,里面定义了两个字段,pid和tgid,其中pid就是这个轻量级进程lwp的id,而tgid是lwp组的id,即主lwp的id

1、pid,tid,真实pid的使用

进程pid: getpid()

线程tid: pthread_self()//进程内唯一,但是在不同进程则不唯一。

线程pid: syscall(SYS_gettid) //系统内是唯一的

#include#include#include#include

structmessage

{inti;intj;

};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[])

{structmessage test;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值