获取进程ID的方法:
我知道和实践过的有这么三种:
- ps -A |grep "cmdname"| awk '{print $1}'
- pidof "cmdname"
- pgrep "cmdname"
这三种在bash和busybox ash里面的运行结果稍有不同,
第一种完全相同,但是因为调用命令次数较多,性能上是不行的。
第二种: pidof 只能获取程序的文件名匹配到的进程号,在ash中 比如 pidof "usr/bin/telnetd" 和 pidof "telnetd"中结果不一样, 前一种结果为空,但是在bash中执行两者一样。
第三种: pgrep跟1的效果相同,因为是单一命令,比第一种性能要好得多。
所以nanoRC就改进为pgrep。
自实现代码:
int Global::find_pid(const char *prg_name)
{
DIR *dir;
struct dirent *entry;
char status[32], buf[1024], *name;
int fd, len, pid, pid_found;
pid_found = 0;
dir = opendir("/proc");
while ((entry = readdir(dir)) != NULL) {
name = entry->d_name;
if (!(*name >= '0' && *name <= '9'))
continue;
pid = atoi(name);
sprintf(status, "/proc/%d/stat", pid);
if((fd = open(status, O_RDONLY)) < 0)
continue;
len = safe_read(fd, buf, sizeof(buf) - 1);
close(fd);
if (len <= 0)
continue;
buf[len] = 0x0;
name = strrchr(buf, ')');
if (name == NULL || name[1] != ' ')
continue;
*name = 0;
name = strrchr(buf, '(');
if (name == NULL)
continue;
if (strncmp(name + 1, prg_name, 16 - 1) == 0) {
pid_found = pid;
break;
}
}
closedir(dir);
return pid_found;
}
实现原理
linux:/proc$ pidof sig_app //获得进程的pid
5049
linux:/proc$ cd 5049
linux:/proc/5049$ ls
attr cpuset io mountinfo pagemap smaps task
auxv cwd latency mounts personality stack wchan
cgroup environ limits mountstats root stat
clear_refs exe loginuid net sched statm
cmdline fd maps oom_adj schedstat status
coredump_filter fdinfo mem oom_score sessionid syscall
linux:/proc/5049$ cat stat //查看stat文件
5049 (sig_app) S 2044 5049 2044 34816 5049 4194304 145 0 0 0 0 0 0 0 20 0 1 0 6109524 1675264 74 4294967295 134512640 134514356 3212917792 3212917448 10843170 0 2 0 2 3222656922 0 0 17 0 0 0 0 0 0