linux通过进程名查找进程,Linux下通过进程名获得进程号
因為存在多進程和線程,Linux下同一個進程名有可能有多個進程號。下面的程序可以一次獲得同一進程名的所有進程號。
process.h
#ifndef __PROCESS_H__
#define __PROCESS_H__
char *basename(const char *path);
int get_pid_by_name(const char* process_name, pid_t pid_list[], int list_size);
int is_process_exist(const char* process_name);
#endif /* __PROCESS_H__ */
process.c (使用/proc/pid/exe 查找進程名有時候會有問題,比如busybox中的命令,查到的是busybox)
修改為使用/proc/pid/cmdline來查找。
#include
#include
#include
#include
#include
#include
#include
#include
char *basename(const char *path)
{
register const char *s;
register const char *p;
p = s = path;
while (*s) {
if (*s++ == '/') {
p = s;
}
}
return (char *) p;
}
/* find all pid of process by name, only compare base name of pid_name
* pid_list: caller malloc pid_t array
* list_size: the size of pid_list
* RETURN:
* < 0: error number
* >=0: how many pid found, pid_list will store the founded pid
*/
int get_pid_by_name(const char* process_name, pid_t pid_list[], int list_size)
{
#define MAX_BUF_SIZE 256
DIR *dir;
struct dirent *next;
int count=0;
pid_t pid;
FILE *fp;
char *base_pname = NULL;
char *base_fname = NULL;
char cmdline[MAX_BUF_SIZE];
char path[MAX_BUF_SIZE];
if(process_name == NULL || pid_list == NULL)
return -EINVAL;
base_pname = basename(process_name);
if(strlen(base_pname) <= 0)
return -EINVAL;
dir = opendir("/proc");
if (!dir)
{
return -EIO;
}
while ((next = readdir(dir)) != NULL) {
/* skip non-number */
if (!isdigit(*next->d_name))
continue;
pid = strtol(next->d_name, NULL, 0);
sprintf(path, "/proc/%u/cmdline", pid);
fp = fopen(path, "r");
if(fp == NULL)
continue;
memset(cmdline, 0, sizeof(cmdline));
if(fread(cmdline, MAX_BUF_SIZE - 1, 1, fp) < 0){
fclose(fp);
continue;
}
fclose(fp);
base_fname = basename(cmdline);
if (strcmp(base_fname, base_pname) == 0 )
{
if(count >= list_size){
break;
}else{
pid_list[count] = pid;
count++;
}
}
}
closedir(dir) ;
return count;
}
/* If process is existed, return true */
int is_process_exist(const char* process_name)
{
pid_t pid;
return (get_pid_by_name(process_name, &pid, 1) > 0);
}
main.c
#include
#include
#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
printf("%u\n", pid[n]);
}
return ret;
}
Makefile:
PROG=check_process
OBJS=process.o main.o
#CFLAGS = -g -ggdb
all:$(PROG)
check_process:$(OBJS)
$(CC) -o $@ $^ $(LDFLAGS)
%.o:%.c
$(CC) -c -o $@ $(CFLAGS) $<
clean:
rm -rf $(PROG) *.o
總結
以上是生活随笔為你收集整理的linux通过进程名查找进程,Linux下通过进程名获得进程号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: clear linux 图形界面,Cle
- 下一篇: linux远程登录uart,Linux