linux 线程id 与进程id对应关系

linux 中的线程是基于进程实现的,每个线程都会有一个进程对应,通过gettid()可以获取到该进程id。
另外,通过pthread_self()获取到的是POSIX thread id。

下面简单举个例子。

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h> 

#define THREAD_STACK_SIZE 128*1024


static void error_handler(const char *arg)
{
    printf(arg);
    exit(EXIT_FAILURE);
}


static void *thread_1(void *arg)
{
    pid_t pid = getpid();
    pthread_t pth_id = pthread_self();
    //pid_t tid = gettid();
    pid_t tid = syscall(SYS_gettid);
    printf("------thread 1 begin pid=%d, tid=%d, thread id=%lu-----\n", pid, tid, pth_id);
    printf("========do a lot of work====\n");

    while(1)
    {
        pause();
    }
    printf("---->thread 1 EXIT\n");
    return;     
}

static void *thread_2(void *arg)
{
    pid_t pid = getpid();
    pthread_t pth_id = pthread_self();
    //pid_t tid = gettid();
    pid_t tid = syscall(SYS_gettid);
    printf("------thread 2 begin pid=%d, tid=%d, thread id=%lu-----\n", pid, tid, pth_id);
    printf("========do a lot of work====\n");

    while(1)
    {
        pause();
    }
    printf("---->thread 2 EXIT\n");
    return; 
}

void test_thread_init(void* thread_func())
{
    pthread_t pthread_id;
    pthread_attr_t attr;

    if (pthread_attr_init(&attr) < 0)
    {
        error_handler("test_thread_init init thread attribute failed\n");
    }

    if (pthread_attr_setstacksize (&attr, THREAD_STACK_SIZE) !=0)
    {
        error_handler("test_thread_init set stack size failed\n");
    }

    if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) < 0)
    {
        error_handler("test_thread_init set setdetachstate failed\n");
    }

    if (pthread_create(&pthread_id, &attr, thread_func, NULL) < 0)
    {
        error_handler("test_thread_init create thread failed\n");
    }

    pthread_attr_destroy(&attr);
    return;
}

int main()
{

    test_thread_init(thread_1);
    test_thread_init(thread_2);

    pid_t pid = getpid();
    pthread_t pth_id = pthread_self();
    //pid_t tid = gettid();
    pid_t tid = syscall(SYS_gettid);
    printf("------thread main begin pid=%d, tid=%d, thread id=%lu-----\n", pid, tid, pth_id);

    while(1)
    {
        pause();
    }
    printf("====>main EXIT\n");
    return 0;
}

测试结果
$ gcc test.c -o test -lpthread

$ ./test
——thread main begin pid=8010, tid=8010, thread id=3075487488—–
——thread 1 begin pid=8010, tid=8011, thread id=3075484480—–
——thread 2 begin pid=8010, tid=8012, thread id=3075349312—–
========do a lot of work====
========do a lot of work====
^C

$ ps -aux | grep test
lanyang 8010 0.0 0.0 2536 316 pts/1 Sl+ 10:30 0:00 ./test

$ ls /proc/8010/task/
8010 8011 8012

从结果可以看到主线程创建了两个线程
3075484480、3075349312对应的进程ID分别为8011、8012.

由于gettid()没有在标准C库中,所以使用时要使用系统调用号去调用。系统调用号可以通过查看文件得到:
/usr/include/asm/unistd.h

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值