美文网首页
Linux中的pid tid thread_id

Linux中的pid tid thread_id

作者: IvanGuan | 来源:发表于2019-01-02 18:17 被阅读0次

 你是不是也被pid,tid,thread_id 这三个东西弄晕了。每个进程都有数自己的pid,可以用getpid()得到的。同样在我们调用pread_create的系统也线程分配了thread_id,可以使用pthread_self()得到。但是除了这两个id还有一个id比较特殊即线程的PID。在内核中,每个线程都有自己的PID,要得到线程的PID,必须用syscall(SYS_gettid);

  • 实验1
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>

struct message
{
    int i;
    int j;
};

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[])
{
    struct message test;
    pthread_t thread_id;
    test.i=10;
    test.j=20;
    pthread_create(&thread_id,NULL,hello,&test);
    printf("parent, the tid=%lu, pid=%d\n",pthread_self(),syscall(SYS_gettid));
    printf("parent, getpid()=%d\n",getpid());
    pthread_join(thread_id,NULL);
    return 0;
}

gcc pid_tid.c -lpthread

./a.out
parent, the tid=140073681307456, pid=17540
parent, getpid()=17540
child, the tid=140073672972032, pid=17541
the arg.i is 10, arg.j is 20
child, getpid()=17540

ps -ef |grep a.out
root     17540 10972 97 18:06 pts/1    00:01:28 ./a.out

top -H -p 17540

KiB Swap:  9727996 total,  9727996 free,        0 used.  6527404 avail Mem
PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
17541 root      20   0   14656    380    296 R 93.8  0.0   0:28.38 a.out
17540 root      20   0   14656    380    296 S  0.0  0.0   0:00.00 a.out

从上例中看出为父进程和子线程的pid均为17540,而子线程的内核pid为17541

 pthread_self函数获取的是线程ID,线程ID在某进程中是唯一的,在不同的进程中创建的线程可能出现ID值相同的情况

  • 实验2
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>

void *thread_one()
{
    printf("thread_one:int %d main process, the tid=%lu,pid=%ld\n",getpid(),pthread_self(),syscall(SYS_gettid));
}

void *thread_two()
{
    printf("thread two:int %d main process, the tid=%lu,pid=%ld\n",getpid(),pthread_self(),syscall(SYS_gettid));
}

int main(int argc, char *argv[])
{
    pid_t pid;
    pthread_t tid_one,tid_two;
    if((pid=fork())==-1)
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    else if(pid==0)
    {
        pthread_create(&tid_one,NULL,(void *)thread_one,NULL);
        pthread_join(tid_one,NULL);
    }
    else
    {
        pthread_create(&tid_two,NULL,(void *)thread_two,NULL);
        pthread_join(tid_two,NULL);
    }
    wait(NULL);
    return 0;
}

[root@xt2 thread]# gcc pid_tid2.c -lpthread
[root@xt2 thread]# ./a.out
thread two:int 18674 main process, the tid=139839637239552,pid=18676
thread_one:int 18675 main process, the tid=139839637239552,pid=18677

参考文章

<1> https://www.cnblogs.com/lakeone/p/3789117.html

相关文章

  • Linux中的pid tid thread_id

     你是不是也被pid,tid,thread_id 这三个东西弄晕了。每个进程都有数自己的pid,可以用getpid...

  • CPU占用问题排查

    top 查到pid ps aux|grep pid 显示线程列表 ps -mp pid -o THREAD,tid...

  • pidstat

    列出pid对应的每个线程占用cpu的情况pidstat -t -p ${pid} 输出格式:TGID TID %u...

  • 获取线程TiD与进程PiD

    /** * Returns the identifier of this process's user....

  • Linux中找出Java程序占用大量CPU的元凶

    原理 通过top找到占用CPU高的进程pid,通过ps找到该进程中占用CPU高的线程tid,最后通过jstack找...

  • task_struct结构

    进程标识符(PID) Unix系统通过pid来标识进程,linux把不同的pid与系统中每个进程或轻量级线程关联,...

  • 了解Kubernetes

    了解Linux中的六种namespace linux实现了mount、UTS、IPC、network、pid、us...

  • 初识k8s

    了解Linux中的六种namespace linux实现了mount、UTS、IPC、network、pid、us...

  • zombies 和 init process

    进程关系 zombies进程 linux中的pid号和file的fd号的规则不同,pid的增长是逐次递增使用,如果...

  • 线上java程序CPU占用过高问题排查

    top 命令查看CPU、内存等使用情况 定位问题线程 ps -mp pid -o THREAD,tid,time ...

网友评论

      本文标题:Linux中的pid tid thread_id

      本文链接:https://www.haomeiwen.com/subject/mxctrqtx.html