Liunx lookup process PID through the process name can be found by pidof [process name]. Conversely, there is no relevant command for the same search process name through PID. In the Linux root directory, there is a VFS (virtual file system) in/proc, and all processes currently running by the system correspond to a process-pid-named folder under that directory, which holds the N-much information that the process is running. There is a status file, cat displays the file, and the first row of name is the process name.
Open the StarDict program, the process name is stardict;
In the shell, we get the process name according to PID and get PID according to the process name respectively
1) to find StarDict pid:pidof stardict
2 according to 1) the PID lookup process name: grep "Name:"/proc/5884/status
Application: Kill a process needs to specify the PID of the process, so we need to first find the PID according to the process name, and then kill;
The Killall command requires only a given process name, which encapsulates the process.
C program to achieve the above process
Copy Code code as follows:
#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];//size arbitrary, can install the path of the CmdLine file
Char cur_task_name[50];//the size of the arbitrary, can be installed to identify the command line text can be
Char Buf[buf_size];
dir = Opendir ("/proc"); Open path
if (NULL!= dir)
{
while (ptr = Readdir (dir))!= NULL)//loop read every file/folder under the path
{
If the "." is read. or ".." Skip, read not folder name and Skip
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);//generate the path to the file to be read
FP = fopen (filepath, "R");/Open 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, PID of the process) if the file content satisfies the requirement
if (!strcmp (Task_name, Cur_task_name))
printf ("PID:%s\n", ptr->d_name);
Fclose (FP);
}
}
Closedir (dir);//close 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:%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);
}
Run Result:
Enter/proc/9674/status to view the contents of the file, all corresponding.
Copy Code code as follows:
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