免费注册 查看新帖 |

Chinaunix

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

如何检测进程是否存在? [复制链接]

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

如何检测进程是否存在?

  1. Andrew Gierth <andrew@erlenstar.demon.co.uk>;

  2. 一种办法是读取/proc接口提供的信息

  3. --------------------------------------------------------------------------
  4. /* gcc -Wall -O3 -o getpid getpid.c */
  5. #include <stdio.h>;
  6. #include <stdlib.h>;
  7. #include <sys/types.h>;
  8. #include <sys/stat.h>;
  9. #include <sys/procfs.h>;
  10. #include <unistd.h>;
  11. #include <stropts.h>;
  12. #include <dirent.h>;
  13. #include <fcntl.h>;

  14. static pid_t getpidbyname ( char * name, pid_t skipit )
  15. {
  16.     DIR *           dirHandle;  /* 目录句柄   */
  17.     struct dirent * dirEntry;   /* 单个目录项 */
  18.     prpsinfo_t      prp;
  19.     int             fd;
  20.     pid_t           pid = -1;

  21.     if ( ( dirHandle = opendir( "/proc" ) ) == NULL )
  22.     {
  23.         return( -1 );
  24.     }
  25.     chdir( "/proc" );  /* 下面使用相对路径打开文件,所以必须进入/proc */
  26.     while ( ( dirEntry = readdir( dirHandle ) ) != NULL )
  27.     {
  28.         if ( dirEntry->;d_name[0] != '.' )
  29.         {
  30.             /* fprintf( stderr, "%s\n", dirEntry->;d_name ); */
  31.             if ( ( fd = open( dirEntry->;d_name, O_RDONLY ) ) != -1 )
  32.             {
  33.                 if ( ioctl( fd, PIOCPSINFO, &prp ) != -1 )
  34.                 {
  35.                     /* fprintf( stderr, "%s\n", prp.pr_fname ); */
  36.                     if ( !strcmp( prp.pr_fname, name ) )  /* 这里是相对路径,而且
  37. 不带参数 */
  38.                     {
  39.                         pid = ( pid_t )atoi( dirEntry->;d_name );
  40.                         if ( skipit != -1 && pid == skipit )  /* -1做为无效pid对
  41. 待 */
  42.                         {
  43.                             pid = -1;
  44.                         }
  45.                         else  /* 找到匹配 */
  46.                         {
  47.                             close( fd );
  48.                             break;  /* 跳出while循环 */
  49.                         }
  50.                     }
  51.                 }
  52.                 close( fd );
  53.             }
  54.         }
  55.     }  /* end of while */
  56.     closedir( dirHandle );
  57.     return( pid );
  58. }  /* end of getpidbyname */

  59. static void usage ( char * arg )
  60. {
  61.     fprintf( stderr, " Usage: %s <proc_name>;\n", arg );
  62.     exit( EXIT_FAILURE );
  63. }  /* end of usage */

  64. int main ( int argc, char * argv[] )
  65. {
  66.     pid_t pid;

  67.     if ( argc != 2 )
  68.     {
  69.         usage( argv[0] );
  70.     }
  71.     pid = getpidbyname( argv[1], -1 );
  72.     if ( pid != -1 )
  73.     {
  74.         fprintf( stderr, "[ %s ] is: <%u>;\n", argv[1], ( unsigned int )pid );
  75.         exit( EXIT_SUCCESS );
  76.     }
  77.     exit( EXIT_FAILURE );
  78. }  /* end of main */
  79. --------------------------------------------------------------------------

  80. 这种技术要求运行者拥有root权限,否则无法有效获取非自己拥有的进程PID。注意
  81. 下面的演示

  82. # ps -f -p 223

  83. UID   PID  PPID  C    STIME TTY      TIME CMD
  84. root   223     1  0   3月 09 ?        0:00 /usr/sbin/vold

  85. # ./getpid /usr/sbin/vold  <-- 这个用法无法找到匹配
  86. # ./getpid vold            <-- 只能匹配相对路径
  87. [ vold ] is: <223>;

  88. 当然你可以自己修改、增强程序,使之匹配各种命令行指定,我就不替你做了。上述
  89. 程序在32-bit kernel的Solaris 2.6和64-bit kernel的Solaris 7上均测试通过。
复制代码

论坛徽章:
0
12 [报告]
发表于 2003-12-03 08:41 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
13 [报告]
发表于 2003-12-03 09:13 |只看该作者

如何检测进程是否存在?

谢谢各位,我现在就去试试!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP