免费注册 查看新帖 |

Chinaunix

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

如何通过子进程号得到父进程号,不通ps! [复制链接]

论坛徽章:
0
21 [报告]
发表于 2003-10-31 07:54 |只看该作者

如何通过子进程号得到父进程号,不通ps!

这个应该是AIX专有的函数。

论坛徽章:
0
22 [报告]
发表于 2003-11-05 22:24 |只看该作者

如何通过子进程号得到父进程号,不通ps!

以下代码在SOLARIS平台上通过,不知道AIX上是否适用
  1. #include <stdio.h>;
  2. #include <unistd.h>;
  3. #include <stropts.h>;
  4. #include <sys/old_procfs.h>;
  5. #include <sys/fcntl.h>;

  6. main(int argc, char **argv)
  7. {
  8.   char path[512];
  9.   int fp;
  10.   prpsinfo_t psinfo;
  11.   prusage_t psusage;

  12.   if (argc != 2) { fprintf(stderr, "Usage: %s <pid>;\n", argv[0]);  exit(-1); }

  13.   sprintf(path, "/proc/%s", argv[1]);
  14.   if ((fp = open(path, O_RDONLY)) < 0) {
  15.         fprintf(stderr, "open file error:%s\n", path);
  16.         exit(-1);
  17.   }

  18.   if (ioctl(fp, PIOCPSINFO, &psinfo) < 0) {
  19.         close(fp);
  20.         exit(-1);
  21.   } //获取进程信息

  22.   if (ioctl(fp, PIOCUSAGE, &psusage) < 0) {
  23.         close(fp);
  24.         exit(-1);
  25.   } //获取进程的资源使用情况

  26.   printf("Command: %s\n", psinfo.pr_psargs);
  27.   printf("PPID: %d\n", psinfo.pr_ppid);
  28. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP