Linux gets pid based on pid process name and pid of c

  • 2020-04-02 02:03:45
  • OfStack

Finding a process PID by process name in Liunx can be found by pidof [process name]. Conversely, there is no command to find the process name through the same PID. In the Linux root directory, there is a /proc VFS(virtual file system), and all processes currently running on the system correspond to a folder named after the process PID in that directory, which holds N more information about the process running. There is a status file that cat displays, and the Name on the first line is the Name of the process.

Open the stardict program, the process is called stardict;

In the shell, get the process name according to the Pid, and get the Pid according to the process name

1) find the pidof stardict: pidof stardict

2) according to the pid of 1) find process Name: grep "Name:" /proc/5884/status

Application: kill a process needs to specify the pid of the process, so we need to find the pid according to the process name, and then kill;
The killall command simply needs to be given a process name, which should encapsulate the process.

Implement the above procedure in C program


#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];//The size is arbitrary, can hold the path of cmdline file
    char cur_task_name[50];//The size is arbitrary, can hold to recognize the command line text
    char buf[BUF_SIZE];
    dir = opendir("/proc"); //Open the path to the
    if (NULL != dir)
    {
        while ((ptr = readdir(dir)) != NULL) //Loop reads each file/folder in the path
        {
            //If it reads "." or ".." Skip, and skip the folder name if it is not read
            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);//Generates the path to the file to be read
            fp = fopen(filepath, "r");//Open the file
            if (NULL != fp)
            {
                if( fgets(buf, BUF_SIZE-1, fp)== NULL ){
                fclose(fp);
                continue;
             }
            sscanf(buf, "%*s %s", cur_task_name);

                //Print the name of the path (that is, the PID of the process) if the file content meets the requirement
                if (!strcmp(task_name, cur_task_name))
                printf("PID:  %sn", ptr->d_name);
                fclose(fp);
            }

        }
        closedir(dir);//Shut down the path
    }
}
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:%dn", pid);
    getNameByPid(pid, task_name);

    
    printf("task name is %sn", task_name);
    getPidByName(task_name);
    sleep(15);
}

Operation results:

Go to /proc/9674/status to view the contents of the file, everything corresponds.


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


Related articles: