tid,pid,ppid,tgid,pgid,uid,euid介绍

PID : 在Unix和Linux系统中,每个运行的程序都是一个进程。每个进程都有一个唯一的进程IDProcess Identification)

PPID : 指的是父进程的pid,每个进程都可以创建一个或多个子进程,对于创建这个子进程的进程,我们称之为父进程。

TID:创建的线程的ID

TGID:线程组ID,包含多个线程的线程组

PGID:进程组ID,包含

UID:用户身份证明(User Identification)的缩写,UID用户在注册后,系统会自动的给你一个UID的数值,只有进程的创建者和 root 用户才有权利对该进程进行操作

EUID:除了UID外,Linux 还为进程保存了一个“有效用户 ID 号”,被称作 EUID,用来确定进程对某些资源和文件的访问权限。在绝大部分情况下,进程的 UID 和 EUID 是一样的

1. 获取PID PPID

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>

void Test_get_pid_ppid(){
    printf( "本进程ID: %d\n", getpid() );
    printf( "父进程PPID: %d\n", getppid() );
    while(1){
        sleep(1);
    }
}

int main(int argc, char* argv[]) {
    Test_get_pid_ppid();
    return 0;
}

输出结果
# 本进程ID: 3926
# 父进程PPID: 5772

# ps -ef | grep test //通过系统命令查看当前pid ppid
# 3926  5772  0 14:15 pts/2    00:00:00 ./test  

那ppid是什么?
# ps -p 5572
# PID TTY          TIME CMD
# 5772 pts/2    00:00:00 bash  //是我当前运行的bash终端

2. 获取tid,tgid

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

#include <sys/syscall.h>

pid_t gettid() { 
    return syscall(__NR_gettid); 
}


void Test_get_pid_ppid(){
    printf( "本进程ID: %d\n", getpid() );
    printf( "父进程PPID: %d\n", getppid() );
}

void *thread_loop1(void *arg){
    printf("thread_loop1 pid = %d\n",getpid());
    printf("thread_loop1 tid = %d\n",gettid());
    printf("thread_loop1 ppid = %d\n",getppid());
    while(1){
        sleep(1);
    }
    return NULL;
}
void *thread_loop2(void *arg){
    printf("thread_loop2 pid = %d\n",getpid());
    printf("thread_loop2 tid = %d\n",gettid());
    printf("thread_loop2 ppid = %d\n",getppid());
    while(1){
        sleep(1);
    }
    return NULL;
}

void Test_tid_tgid(){
    pthread_t tid1;
    pthread_t tid2;
    pthread_create(&tid1, NULL, thread_loop1, NULL); 
    pthread_create(&tid2, NULL, thread_loop2, NULL);
}
int main(int argc, char* argv[]) {
    Test_get_pid_ppid();
    Test_tid_tgid();
    while(1){ 
        sleep(100); 
    };
    return 0;
}

 先执行看结果 :

# gcc pid.c -pthread -o pidtest

本进程ID: 10347
父进程PPID: 5772


thread_loop1 pid = 10347
thread_loop1 tid = 10348
thread_loop1 ppid = 5772


thread_loop2 pid = 10347
thread_loop2 tid = 10349
thread_loop2 ppid = 5772
 

 

1. 通过pstree 查看结构
# pstree -p 10347
# pidtest(10347)─┬─{pidtest}(10348)
                 └─{pidtest}(10349)
清楚的看到10347进程对应了两个线程,tid分别为10348 10349


2. ps -T -eo comm,pid,tid,ppid,tgid,pgid,uid | grep pidtest

# COMMAND           PID   TID  PPID  TGID  PGID   UID
# pidtest         10347 10347  5772 10347 10347  1000
# pidtest         10347 10348  5772 10347 10347  1000
# pidtest         10347 10349  5772 10347 10347  1000


Note:
1. 主进程和线程 pid 相同,等于主进程pid
2. 主进程和线程 ppid 相同,等于主进程ppid
3. 主进程和线程 tgid 相同,等于主进程pid
4. 主进程和线程 pgid相同,等于主进程pid

3. fork子进程

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

#include <sys/syscall.h>

pid_t gettid() { 
    return syscall(__NR_gettid); 
}


void Test_get_pid_ppid(){
    printf("--------%s-------------\n",__func__);
    printf( "本进程ID: %d\n", getpid() );
    printf( "父进程PPID: %d\n", getppid() );
}

void *thread_loop1(void *arg){
    printf("thread_loop1 pid = %d\n",getpid());
    printf("thread_loop1 tid = %d\n",gettid());
    printf("thread_loop1 ppid = %d\n",getppid());
    while(1){
        sleep(1);
    }
    return NULL;
}
void *thread_loop2(void *arg){

    printf("thread_loop2 pid = %d\n",getpid());
    printf("thread_loop2 tid = %d\n",gettid());
    printf("thread_loop2 ppid = %d\n",getppid());
    while(1){
        sleep(1);
    }
    return NULL;
}

void Test_tid_tgid(){
    pthread_t tid1;
    pthread_t tid2;
    printf("--------%s-------------\n",__func__);
    pthread_create(&tid1, NULL, thread_loop1, NULL); 
    pthread_create(&tid2, NULL, thread_loop2, NULL);
}

int Test_fork(){
    pid_t pid;
    printf("--------%s-------------\n",__func__);
    pid = fork();
    if (pid == -1) {
        // 创建子进程失败
        perror("fork");
        return 1;
    } else if (pid == 0) {
        // 子进程代码
        
        printf("子进程(PID:%d)\n", getpid());
        printf("子进程 tid = %d\n",gettid());
        printf("子进程 ppid = %d\n",getppid());
        while(1){
            sleep(10);
        }
    } else {
        // 父进程代码
        printf("父进程(PID:%d),创建了子进程(PID:%d)\n", getpid(), pid);
        printf("父进程 tid = %d\n",gettid());
        printf("父进程 ppid = %d\n",getppid());
    }
}

int main(int argc, char* argv[]) {
    Test_get_pid_ppid();
    Test_tid_tgid();
    Test_fork();
    while(1){ 
        sleep(100); 
    };
    return 0;
}

测试结果:

--------Test_get_pid_ppid-------------
本进程ID: 14782
父进程PPID: 5772
--------Test_tid_tgid-------------
--------Test_fork-------------
thread_loop1 pid = 14782
thread_loop1 tid = 14783
thread_loop1 ppid = 5772
父进程(PID:14782),创建了子进程(PID:14785)
父进程 tid = 14782
父进程 ppid = 5772
thread_loop2 pid = 14782
thread_loop2 tid = 14784
thread_loop2 ppid = 5772
子进程(PID:14785)
子进程 tid = 14785
子进程 ppid = 14782

# ps -T -eo comm,pid,tid,ppid,tgid,pgid,uid
# COMMAND           PID   TID  PPID  TGID  PGID   UID
# pidtest         14782 14782  5772 14782 14782  1000
# pidtest         14782 14783  5772 14782 14782  1000
# pidtest         14782 14784  5772 14782 14782  1000
# pidtest         14785 14785 14782 14785 14782  1000

Note:
1. 主进程14782 创建的进程pid=14785
2. 子进程的tid = pid
3. 子进程的ppid = 主进程pid 
4. 子进程的tgid = 自身pid
5. 子进程pgid = 主进程pid

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打个工而已

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值