| zjBahamout 回复于:2003-10-23 09:23:52
|
getppid()
|
| hjqq 回复于:2003-10-23 09:28:32
|
不对,我是说不在子程序中,在别的程序中怎么知道一个进程的父进程,其实就是ps的实现.
|
| zjBahamout 回复于:2003-10-23 09:32:49
|
写个LKM吧
|
| hjqq 回复于:2003-10-23 09:33:55
|
说具体点,我看不懂啊
|
| qjlemon 回复于:2003-10-23 10:56:55
|
如果有/proc文件系统的话可以去那里找一找,应该有这方面的信息,但具体做法每个系统都不同。
|
| 思一克 回复于:2003-10-23 10:59:58
|
很简单:
cat /proc/sub-pid/stat
可以看到父进程的号码
|
| qjlemon 回复于:2003-10-23 11:03:00
|
举例:
在FreeBSD里看103进程的父进程(应该是102):
[root@bsd]# ps -axj|grep sh
root 102 76 76 c10b7cc0 0 S ?? 0:02.86 sshd: lemon@ttyp0 (sshd
lemon 103 102 103 c10e3580 0 Is p0 0:00.28 -sh (sh)
[root@bsd]# cd /proc/103
[root@bsd]# cat status
sh 103 102 103 103 5,0 ctty,sldr 1066872731,634443 0,0 0,284271 wait 1001 1001 1001,1001,1001,0 -
|
| hjqq 回复于:2003-10-23 11:05:58
|
多谢几位老兄,但是没有/proc文件系统,同时我是在aix系统下好像ps -axj不能用啊
|
| qjlemon 回复于:2003-10-23 11:14:28
|
aix啊,换!换成FreeBSD!
aix里ps的参数不一样。我也不知道在aix里怎么做,aix4里确实没有/proc文件系统。
|
| hjqq 回复于:2003-10-23 11:17:37
|
多谢了,但不能换啊,在RS6000上啊,
|
| qjlemon 回复于:2003-10-23 11:24:19
|
当然说“换”是开玩笑啦,不知道aix 5有没有/proc, 或者有其它办法解决。
不知能不能找到aix下ps的源代码?
|
| hjqq 回复于:2003-10-23 11:28:20
|
哦,我没找到啊
|
| gadfly 回复于:2003-10-23 21:05:54
|
AIX上有个的rpm的port工具。也可以安装rpm软件包的。
所以 你可以到rpmfind.net上下载相应的软件源码参考
|
| 蓝色键盘 回复于:2003-10-24 11:46:39
|
aix的ps是能够看到父进程的。
如果那个号码的进程号的进程是你自己启动的,那么问题好解决。
如果不是你自己的,也没有权限的话。
采用楼上几位兄台的办法。
|
| wwc 回复于:2003-10-25 13:05:00
|
在aix 5L中是有/proc文件系统的,不过在aix有很简单的方法得到进程信息
int GetPPID(pid_t pid)
{
struct procsinfo p;
int i;
i=0;
while(getprocs(&p,sizeof(p),NULL,0,&i,1)>0)
{
if(p.pi_pid==pid)
{
return p.pi_ppid;
}
}
return -1;
}
getprocs是系统函数,具体的头文件记不清了通过man查一下吧。
|
| qjlemon 回复于:2003-10-26 08:46:42
|
不错不错,刚才在AIX4上试了一下:
[code:1:564e93b7d1]
#include <stdio.h>
#include <procinfo.h>
int GetPPID(pid_t pid);
int
main(int argc, char ** argv)
{
printf("%d\n", GetPPID(6482));
}
int
GetPPID(pid_t pid)
{
struct procsinfo p;
int i;
i=0;
while (getprocs(&p,sizeof(p),NULL,0,&i,1) > 0) {
if (p.pi_pid == pid) {
return p.pi_ppid;
}
}
return -1;
}
[/code:1:564e93b7d1]
|
| hjqq 回复于:2003-10-29 12:24:12
|
pid_t GetPpid(pid_t pid)
{
struct procsinfo ProcessBuffer;
int ProcessSize;
pid_t IndexPointer;
int ret;
IndexPointer = pid;
ret = getprocs(&ProcessBuffer,sizeof(struct procsinfo),0,0,&IndexPointer,1);
if(ret<0) {
printf("\nerror.");
return -1;
}
/* printf("\nProcessBuffer.pi_ppid=[%ld]",ProcessBuffer.pi_ppid); */
return(ProcessBuffer.pi_ppid);
}
|
| hjqq 回复于:2003-10-30 16:16:00
|
pid_t GetPpid(pid_t pid)
{
struct procsinfo ProcessBuffer;
int ProcessSize;
pid_t IndexPointer;
int ret;
IndexPointer = pid;
ret = getprocs(&ProcessBuffer,sizeof(struct procsinfo),0,0,&IndexPointer,1);
if(ret<0) {
printf("\nerror.");
return -1;
}
/* printf("\nProcessBuffer.pi_ppid=[%ld]",ProcessBuffer.pi_ppid); */
return(ProcessBuffer.pi_ppid);
}
|
| freecoder 回复于:2003-10-30 17:37:32
|
linux 中好像没有getprocs这个函数。。。。
|
| qjlemon 回复于:2003-10-31 07:54:57
|
这个应该是AIX专有的函数。
|
| chdonald 回复于:2003-11-05 22:24:01
|
以下代码在SOLARIS平台上通过,不知道AIX上是否适用
[code:1:08cbbe3962]#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);
}[/code:1:08cbbe3962]
|