linux 线程tid,linux中线程的pid,线程的tid和线程pid

本文详细介绍了在Linux系统中如何获取进程ID(PID)和线程ID(TID)。通过使用getpid()函数获取进程ID,pthread_self()获取线程ID,以及syscall(SYS_gettid)获取线程ID。示例代码展示了主线程和子线程打印各自ID的差异,强调主线程的PID与进程PID相同,可用于判断是否为主线程。
摘要由CSDN通过智能技术生成

在linux系统中,

每个进程有一个pid(进程ID),获取函数:getpid()

每个线程有一个tid(线程ID),获取函数:pthread_self()

每个线程有一个pid(不知道叫什么),获取函数:syscall(SYS_gettid)

程序举例:

void *hello(void *arg)

{

printf("%ld\n", syscall(SYS_gettid));

printf("%d\n", getpid());

printf("%ld\n", pthread_self());

}

int main()

{

printf("%ld\n", syscall(SYS_gettid));

printf("%d\n", getpid());

printf("%ld\n", pthread_self());

printf("------------------------------\n");

pthread_t pthread_id;

pthread_create(&pthread_id, NULL, hello, NULL);

sleep(2);

printf("------------------------------\n");

printf("%ld\n", syscall(SYS_gettid));

printf("%d\n", getpid());

printf("%ld\n", pthread_self());

return 0;

}

结果显示

edf7f45446c8852d702b61e0f7eebca2.png

注意:主线程的pid和所在进程的pid一致,可以通过这个来判断是否是主线程

标签:ld,syscall,printf,pid,线程,pthread,tid

来源: https://blog.csdn.net/qq_34489443/article/details/100585685

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值