[英]Determining if a process is a child or parent process (not using return value of fork)
How to write a programm to show whether it's a child or parent process after the fork()
is used? 使用
fork()
后,如何编写程序以显示它是子进程还是父进程?
I need to find other way then checking the return value of fork()
or getpid()
function. 我需要找到其他方法,然后检查
fork()
或getpid()
函数的返回值。
I know eg that process resource utilizations ( getrusage(2)
) and CPU time counters ( times(2)
) are reset to zero in the child. 我知道例如,子进程中的进程资源利用率(
getrusage(2)
)和CPU时间计数器( times(2)
)被重置为零。 How i can use for example that knowledge (or other child-parents differences) to determine my problem? 例如,我如何使用该知识(或其他父母与子女之间的差异)来确定我的问题?
Thank you for your help! 谢谢您的帮助!
Setup a signal handler for SIGCHLD
in one of the processes and end the other one. 在其中一个过程中为
SIGCHLD
设置信号处理程序,然后结束另一个过程。 If the SIGCHLD
handler gets called you know the child died. 如果
SIGCHLD
处理程序被调用,您就知道孩子已经死亡。 If not the parent died. 如果不是,父母就死了。
Another (less distructive) way it to call wait()
. 调用
wait()
另一种(破坏性较小)的方式。 If it returns ECHLD
no child is around, thus the calling process is the child. 如果返回
ECHLD
没有子进程存在,因此调用过程就是该子进程。 If it blocks, at least one child is around, thus the calling process is the parent. 如果阻塞,则至少有一个孩子在附近,因此调用进程是父进程。
Or call waitpid()
with PID of the other process. 或使用其他进程的PID调用
waitpid()
。 The same logic on the result as for wait()
applies. 结果与
wait()
逻辑相同。
You could exploit the fact that the parent will be a process group leader (and thus its pid will be the same as its process group id) while the child will be a member of that process group and thus its pid will be different from the process group id: 您可以利用以下事实:父级将成为流程组负责人(因此其pid将与其流程组ID相同),而子级将成为该流程组的成员,因此其pid将与流程不同群组编号:
#include <stdio.h>
#include <unistd.h>
main(int argc, char **argv) {
char *self = NULL;
fork(); /* looking at return value would be cheating */
int pid = getpid();
int pgid = getpgid(0);
if (pid == pgid)
self = "Parent";
else
self = "Child";
printf("%s pid = %d pgid = %d\n", self, pid, pgid);
}
Running yields: 运行收益:
$ ./pgid
Parent pid = 29400 pgid = 29400
Child pid = 29401 pgid = 29400
This does of course still use getpid()
but not in the same way you describe above. 当然,这确实仍然使用
getpid()
但使用的方式与您上面描述的方式不同。
On Linux, you might also use proc(5) . 在Linux上,您也可以使用proc(5) 。 The process of pid 1234 is described by the directory
/proc/1234/
and you could read sequentially and parse the /proc/1234/status
pseudo-file to get all that information. pid 1234的过程由目录
/proc/1234/
,您可以顺序阅读并解析/proc/1234/status
伪文件以获取所有信息。
To understand a bit more, read the proc(5) man page, then type 要了解更多信息,请阅读proc(5)手册页,然后键入
cat /proc/self/status
cat /proc/$$/status
cat /proc/self/maps
cat /proc/$$/maps
in a terminal, and try to understand the output. 在终端中,并尝试了解输出。 You might parse it programmatically (eg with
FILE*f = fopen("/proc/1234/status","r");
then do some fgets
or fscanf
on f
; then fclose(f);
) 您可以通过编程方式对其进行解析(例如,使用
FILE*f = fopen("/proc/1234/status","r");
然后对f
执行一些fgets
或fscanf
;然后执行fclose(f);
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.