免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: keanlee
打印 上一主题 下一主题

有没有系统调用可以判断某进程是否存在?  关闭 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2005-05-27 11:00 |只看该作者

有没有系统调用可以判断某进程是否存在?

一个程序名可以对应多个PID。
如在kernel中判断程序是否还在运行是可以的。traverse tasklist, 查mm->;arg_start看有无匹配的名字。
在用户状态,看/proc/XXX/cmdline. XXX是那些进程数字。

论坛徽章:
0
12 [报告]
发表于 2005-05-27 11:29 |只看该作者

有没有系统调用可以判断某进程是否存在?

忽然想到这个问题(想在程序中知道一个进程是否已纪存在,但是只知道该进程的名字,而不知道进程pid),试着解决(当然最笨的方法是ps -ef >; test.txt,然后去解析这个文件),没有成功,于是就问一下.

论坛徽章:
0
13 [报告]
发表于 2005-05-27 12:14 |只看该作者

有没有系统调用可以判断某进程是否存在?

You really *don't* want to do this.

The most portable way, by far, is to do `popen(pscmd, "r"' and parse the
output. (pscmd should be something like `"ps -ef"' on SysV systems; on BSD
systems there are many possible display options: choose one.)

In the examples section, there are two complete versions of this; one for
SunOS 4, which requires root permission to run and uses the `kvm_*'
routines to read the information from kernel data structures; and another
for SVR4 systems (including SunOS 5), which uses the `/proc' filesystem.

It's even easier on systems with an SVR4.2-style `/proc'; just read a
psinfo_t structure from the file `/proc/PID/psinfo' for each PID of
interest. However, this method, while probably the cleanest, is also
perhaps the least well-supported. (On FreeBSD's `/proc', you read a
semi-undocumented printable string from `/proc/PID/status'; Linux has
something similar.)

论坛徽章:
0
14 [报告]
发表于 2005-06-01 15:38 |只看该作者

有没有系统调用可以判断某进程是否存在?

我就是用用popen执行ps命令,实现的,你是要控制程序冲入吧,可以采用加锁的方式实现的

论坛徽章:
0
15 [报告]
发表于 2005-06-01 17:08 |只看该作者

有没有系统调用可以判断某进程是否存在?

目前似乎是没有很方便的法子, 我看过pidof的源码, 它是遍历/proc
查看这个文件: /proc/3135/stat的第二段内容, 如:
3135 (vmware) S 3125 3125 2256 34818 3669 8192 8682 39508 155 2014 560 76 448 10674 15 0 1 0 1451896 43618304 2155 4294967295 134512640 140840648 3221222960 3221221100 3071775961 0 0 3149824 332001007 0 0 0 17 0 0 0
用readlink(2)来看 /proc/3135/exe也行, 不过当你权限不够时就不可以了.

论坛徽章:
0
16 [报告]
发表于 2005-06-01 17:33 |只看该作者

有没有系统调用可以判断某进程是否存在?

ps -ea|awk -v prgname="yourname" '
$4==prgname{
print $1
}'

返回的是程序的pid,如果有多个程序叫yourname,则返回多行
一行为一个pid

论坛徽章:
0
17 [报告]
发表于 2005-06-01 18:03 |只看该作者

有没有系统调用可以判断某进程是否存在?

给一个我们用的例子,其实就是仿照busybox里面的code改写。

  1. pid_t* find_pid_by_name( char* pidName, int *count)
  2. {
  3.         DIR *dir;
  4.         struct dirent *next;

  5.         pid_t   *pidList;
  6.         int i=0,n=0,j=0;
  7.        
  8.         dir = opendir("/proc");
  9.         if (!dir)
  10.         {
  11.                 printf("find_pid_by_name: Cannot open /proc");
  12.                 exit(1);
  13.         }
  14.         pidList = malloc(sizeof(*pidList)*5);
  15.         while ((next = readdir(dir)) != NULL) {
  16.                 FILE *status;
  17.                 char filename[READ_BUF_SIZE];
  18.                 char buffer[READ_BUF_SIZE];
  19.                 char name[READ_BUF_SIZE];
  20.                
  21.                 /* Must skip ".." since that is outside /proc */
  22.                 if (strcmp(next->;d_name, "..") == 0)
  23.                         continue;
  24.                
  25.                 /* If it isn't a number, we don't want it */
  26.                 if (!isdigit(*next->;d_name))
  27.                         continue;
  28.                
  29.                 //sprintf(filename, "/proc/%s/status", next->;d_name);
  30.                 sprintf(filename, "/proc/%s/cmdline", next->;d_name);
  31.                 if (! (status = fopen(filename, "r")) ) {
  32.                         continue;
  33.                 }
  34.                 /*if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) {
  35.                 fclose(status);
  36.                 continue;
  37.         } */
  38.                 n=fread(buffer, 1,READ_BUF_SIZE-1, status);
  39.                 if ( n <= 0)
  40.                 {
  41.                         fclose(status);
  42.                         continue;
  43.                 }

  44.                
  45.                 fclose(status);

  46.                 // Buffer should contain the command  string
  47.                 if ( n>;=4 )
  48.                 {
  49.                         if ( bstrncmp(buffer, pidName,n-1) == 0 )
  50.                         {
  51.                                 pidList[i]=strtol(next->;d_name, NULL, 0);
  52.                                 printf("find_pid_by_name(): PID(%d) match\n",strtol(next->;d_name, NULL, 0));
  53.                                 i++;
  54.                         }
  55.                 }
  56.         }
  57.         closedir(dir) ;
  58.         *count = i;
  59.         printf("find_pid_by_name(): %d\n", *count);
  60.         return pidList;
  61. }
复制代码

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
18 [报告]
发表于 2005-06-01 18:08 |只看该作者

有没有系统调用可以判断某进程是否存在?

如果考虑跨平台的话,好象只能用popen了吧?
ps -e -o comm -o pid | grep procname

论坛徽章:
0
19 [报告]
发表于 2005-06-02 23:31 |只看该作者

有没有系统调用可以判断某进程是否存在?

在linux下,可以读取/proc,在solaris下,可以使用procfs里面提供的方法(具体我记不清了,是个系统调用接口,我用过,你可以找找),别的平台也会有相应的方法,这个“层次”要“低点”吧。呵呵。

论坛徽章:
0
20 [报告]
发表于 2005-06-03 09:53 |只看该作者

有没有系统调用可以判断某进程是否存在?

bufStr="ps -ef|grep -v grep|grep 程序名 |wc -l"
if((fRead=popen(bufStr,"r")!= NULL)
{
while(fgets(bufCmd,256,fRead)!=NULL){
if(atoi(bufCmd)>;0) 程序存在;
                                else 进程未运行;
}
pclose(fRead);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP