linux下通过进程名获得进程号

       这些代码原来是别人委托我开发,用来控制JVM的CPU使用率的, 后来因为种种原因,之开发到一半,所以现在写到博客里了。。。。

 process.h

1 #ifndef PROCESS_H_INCLUDED
2 #define PROCESS_H_INCLUDED
3 char *basename(const char *path);
4 int get_pid_by_name(const char* process_name, pid_t pid_list[], int list_size);
5 int is_process_exist(const char* process_name);
6 #endif // PROCESS_H_INCLUDED

process.c

 1     #include <stdio.h>
 2     #include <stdlib.h>
 3     #include <unistd.h>
 4     #include <string.h>
 5     #include <sys/types.h>
 6     #include <dirent.h>
 7     #include <ctype.h>
 8     #include <errno.h>
 9     char *basename(const char *path)
10     {
11         register const char *s;
12         register const char *p;
13 
14         p = s = path;
15 
16         while (*s) {
17             if (*s++ == '/') {
18                 p = s;
19             }
20         }
21 
22         return (char *) p;
23     }
24 
25      /* 根据进程名称获取PID, 比较 base name of pid_name
26       * pid_list: 获取PID列表
27       * list_size: 获取PID列表长度
28       * RETURN值说明:
29       *              < 0: 
30       *              >=0: 发现多少PID, pid_list 将保存已发现的PID
31       */
32     int get_pid_by_name(const char* process_name, pid_t pid_list[], int list_size)
33     {
34     #define  MAX_BUF_SIZE       256
35 
36         DIR *dir;
37         struct dirent *next;
38         int count=0;
39         pid_t pid;
40         FILE *fp;
41         char *base_pname = NULL;
42         char *base_fname = NULL;
43         char cmdline[MAX_BUF_SIZE];
44         char path[MAX_BUF_SIZE];
45 
46         if(process_name == NULL || pid_list == NULL)
47             return -EINVAL;
48 
49         base_pname = basename(process_name);
50         if(strlen(base_pname) <= 0)
51             return -EINVAL;
52 
53         dir = opendir("/proc");
54         if (!dir)
55         {
56             return -EIO;
57         }
58         while ((next = readdir(dir)) != NULL) {
59             /* skip non-number */
60             if (!isdigit(*next->d_name))
61                 continue;
62 
63             pid = strtol(next->d_name, NULL, 0);
64             sprintf(path, "/proc/%u/cmdline", pid);
65             fp = fopen(path, "r");
66             if(fp == NULL)
67                 continue;
68 
69             memset(cmdline, 0, sizeof(cmdline));
70             if(fread(cmdline, MAX_BUF_SIZE - 1, 1, fp) < 0){
71                 fclose(fp);
72                 continue;
73             }
74             fclose(fp);
75             base_fname = basename(cmdline);
76 
77             if (strcmp(base_fname, base_pname) == 0 )
78             {
79                 if(count >= list_size){
80                     break;
81                 }else{
82                     pid_list[count] = pid;
83                     count++;
84                 }
85             }
86         }
87         closedir(dir) ;
88         return count;
89     }
90 
91     /* 如果进程已经存在, return true */
92     int is_process_exist(const char* process_name)
93     {
94         pid_t pid;
95 
96         return (get_pid_by_name(process_name, &pid, 1) > 0);
97     }

main.c

    #include <stdlib.h>  
    #include <stdio.h>  
    #include "process.h"  
      
    #define MAX_PID_NUM     32  
    int main(int argc, char* argv[])  
    {  
      char* process;  
      int ret = 0;  
      int n;  
      pid_t pid[MAX_PID_NUM];  
      
      if(argc < 2)  
        process = argv[0];  
      else  
        process = argv[1];  
      
      ret = get_pid_by_name(process, pid, MAX_PID_NUM);  
      printf("process '%s' is existed? (%d): %c\n", process, ret, (ret > 0)?'y':'n');  
      for(n=0;n<ret;n++){  
        printf("%u\n", pid[n]);  
      }  
      return ret;  
    }  

Makefile:

 1     PROG=check_process  
 2     OBJS=process.o main.o  
 3     #CFLAGS = -g -ggdb  
 4       
 5     all:$(PROG)  
 6       
 7     check_process:$(OBJS)  
 8         $(CC) -o $@ $^ $(LDFLAGS)  
 9       
10     %.o:%.c  
11         $(CC) -c -o $@ $(CFLAGS) $<  
12       
13     clean:  
14         rm -rf $(PROG) *.o  
原文地址:https://www.cnblogs.com/pd520/p/2771343.html