由进程名获取进程ID的方法及实现

获取进程ID的方法:

我知道和实践过的有这么三种:

  1.  ps -A |grep "cmdname"| awk '{print $1}'
  2.  pidof "cmdname"
  3.  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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值