linux根据pid获取进程名和获取进程pid(c语言获取pid)

 更新时间:2020年4月25日 17:43  点击:1459

Liunx中通过进程名查找进程PID可以通过 pidof [进程名] 来查找。反过来 ,相同通过PID查找进程名则没有相关命令。在linux根目录中,有一个/proc的VFS(虚拟文件系统),系统当前运行的所有进程都对应于该目录下的一个以进程PID命名的文件夹,其中存放进程运行的N多信息。其中有一个status文件,cat显示该文件, 第一行的Name即为进程名。

打开stardict程序,进程名为stardict;

shell中分别根据Pid获取进程名、根据进程名获取Pid

1)查找stardict的pid:pidof stardict

2)根据1)的pid查找进程名: grep "Name:" /proc/5884/status

应用:kill一个进程需要指定该进程的pid,所以我们需要先根据进程名找到pid,然后再kill;
killall命令则只需要给定进程名即可,应该是封装了这个过程。

C程序中实现上述过程

复制代码 代码如下:

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

#define BUF_SIZE 1024

void getPidByName(char* task_name)
{
    DIR *dir;
    struct dirent *ptr;
    FILE *fp;
    char filepath[50];//大小随意,能装下cmdline文件的路径即可
    char cur_task_name[50];//大小随意,能装下要识别的命令行文本即可
    char buf[BUF_SIZE];
    dir = opendir("/proc"); //打开路径
    if (NULL != dir)
    {
        while ((ptr = readdir(dir)) != NULL) //循环读取路径下的每一个文件/文件夹
        {
            //如果读取到的是"."或者".."则跳过,读取到的不是文件夹名字也跳过
            if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0))            
            continue;
            if (DT_DIR != ptr->d_type)
              continue;

            sprintf(filepath, "/proc/%s/status", ptr->d_name);//生成要读取的文件的路径
            fp = fopen(filepath, "r");//打开文件
            if (NULL != fp)
            {
                if( fgets(buf, BUF_SIZE-1, fp)== NULL ){
                fclose(fp);
                continue;
             }
            sscanf(buf, "%*s %s", cur_task_name);

                //如果文件内容满足要求则打印路径的名字(即进程的PID)
                if (!strcmp(task_name, cur_task_name))
                printf("PID:  %s\n", ptr->d_name);
                fclose(fp);
            }

        }
        closedir(dir);//关闭路径
    }
}

void getNameByPid(pid_t pid, char *task_name) {
    char proc_pid_path[BUF_SIZE];
    char buf[BUF_SIZE];

    sprintf(proc_pid_path, "/proc/%d/status", pid);
    FILE* fp = fopen(proc_pid_path, "r");
    if(NULL != fp){
        if( fgets(buf, BUF_SIZE-1, fp)== NULL ){
            fclose(fp);
        }
        fclose(fp);
        sscanf(buf, "%*s %s", task_name);
    }
}

void main(int argc, char** argv)
{
    char task_name[50];
    pid_t pid = getpid();

    printf("pid of this process:%d\n", pid);
    getNameByPid(pid, task_name);

    /*
    strcpy(task_name, argv[0]+2);
    printf("task name is %s\n", task_name);
    getPidByName(task_name);
    */
    printf("task name is %s\n", task_name);
    getPidByName(task_name);
    sleep(15);
}

运行结果:

进入/proc/9674/status查看文件内容,一切对应。

复制代码 代码如下:

Name: test
State: S (sleeping)
Tgid: 9674
Pid: 9674
PPid: 7438
TracerPid: 0
Uid: 1000 1000 1000 1000
Gid: 1000 1000 1000 1000
FDSize: 256
Groups: 4 24 27 30 46 112 124 1000
VmPeak:  4340 kB
VmSize:  4336 kB
VmLck:  0 kB
VmPin:  0 kB
VmHWM:  600 kB
VmRSS:  600 kB
VmData:  184 kB
VmStk:  136 kB
VmExe:  4 kB
VmLib:  1920 kB
VmPTE:  32 kB
VmSwap:  0 kB
Threads: 1
SigQ: 0/15776
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: 0000001fffffffff
Seccomp: 0
Cpus_allowed: f
Cpus_allowed_list: 0-3
Mems_allowed: 00000000,00000001
Mems_allowed_list: 0
voluntary_ctxt_switches: 1
nonvoluntary_ctxt_switches: 4

[!--infotagslink--]

相关文章