- 论坛徽章:
- 0
|
如何通过子进程号得到父进程号,不通ps!
以下代码在SOLARIS平台上通过,不知道AIX上是否适用
- #include <stdio.h>;
- #include <unistd.h>;
- #include <stropts.h>;
- #include <sys/old_procfs.h>;
- #include <sys/fcntl.h>;
- main(int argc, char **argv)
- {
- char path[512];
- int fp;
- prpsinfo_t psinfo;
- prusage_t psusage;
- if (argc != 2) { fprintf(stderr, "Usage: %s <pid>;\n", argv[0]); exit(-1); }
- sprintf(path, "/proc/%s", argv[1]);
- if ((fp = open(path, O_RDONLY)) < 0) {
- fprintf(stderr, "open file error:%s\n", path);
- exit(-1);
- }
- if (ioctl(fp, PIOCPSINFO, &psinfo) < 0) {
- close(fp);
- exit(-1);
- } //获取进程信息
- if (ioctl(fp, PIOCUSAGE, &psusage) < 0) {
- close(fp);
- exit(-1);
- } //获取进程的资源使用情况
- printf("Command: %s\n", psinfo.pr_psargs);
- printf("PPID: %d\n", psinfo.pr_ppid);
- }
复制代码 |
|