Linux gets process name and gets process PID based on PID (C language gets PID) _c language

Source: Internet
Author: User
Tags sprintf strcmp first row

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;
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))
if (Dt_dir!= ptr->d_type)

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);
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:

State:s (sleeping)
uid:1000 1000 1000 1000
gid:1000 1000 1000 1000
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.