C语言 子进程和父进程 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18154296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
child and parent process id
提问by Santosh Sahu
Just got confused with parent pid value in child process block. My program is given below:
刚刚与子进程块中的父 pid 值混淆。我的程序如下:
int main(int argc, char *argv[])
{
pid_t pid;
pid=fork();
if(pid==-1){
perror("fork failure");
exit(EXIT_FAILURE);
}
else if(pid==0){
printf("pid in child=%d and parent=%d\n",getpid(),getppid());
}
else{
printf("pid in parent=%d and childid=%d\n",getpid(),pid);
}
exit(EXIT_SUCCESS);
}
Output: pid in parent=2642 and childid=2643
输出:pid in parent=2642 and childid=2643
pid in child=2643 and parent=1
child=2643 和 parent=1 中的pid
In "Advanced Unix programming" it says that child process can get parent process id using getppid() function. But here I am getting "1" which is "init" process id.
在“高级 Unix 编程”中,它说子进程可以使用 getppid() 函数获取父进程 ID。但在这里我得到“1”,它是“init”进程ID。
How can I get the parent pid value in the child process block.. Please help me in getting output.
如何在子进程块中获取父 pid 值..请帮助我获取输出。
I executed in "Linux Mint OS" but in "WindRiver" OS I am not getting this problem. Does this program change behaviour according to OS?
我在“Linux Mint OS”中执行,但在“WindRiver”操作系统中我没有遇到这个问题。这个程序会根据操作系统改变行为吗?
回答by hek2mgl
That's because the father can / will exit before the son. If a father exists without having requested the return value of it's child, the child will get owned by the process with pid=1. What is on classic UNIX or GNU systems SystemV init.
那是因为父亲可以/将在儿子之前退出。如果父亲在没有请求其孩子的返回值的情况下存在,则孩子将被 pid=1 的进程拥有。什么是经典的 UNIX 或 GNU 系统 SystemV init。
The solution is to use waitpid()in father:
解决方法是waitpid()在father中使用:
int main(int argc, char *argv[])
{
pid_t pid;
pid=fork();
if(pid==-1){
perror("fork failure");
exit(EXIT_FAILURE);
}
else if(pid==0){
printf("pid in child=%d and parent=%d\n",getpid(),getppid());
}
else{
printf("pid in parent=%d and childid=%d\n",getpid(),pid);
}
int status = -1;
waitpid(pid, &status, WEXITED);
printf("The child exited with return code %d\n", status);
exit(EXIT_SUCCESS);
}
回答by David Elliman
After the fork you have two new processes and you can know the child id in the parent but not the other way round. If you really need this you would have to open a pipe (popen) before the fork and then the parent could write this into the pipe and the child could read it.
在 fork 之后,您有两个新进程,您可以知道父进程中的子进程 ID,但反过来不行。如果你真的需要这个,你必须在 fork 之前打开一个管道 (popen),然后父母可以将它写入管道,孩子可以读取它。
回答by Sandeep_black
Once the parent completes it execution and child is still running. Then child is known as orphan (as it's parent died) and it is adopted by init process if you are login by root ( whose pid =1 ).
一旦父级完成它的执行并且子级仍在运行。然后孩子被称为孤儿(因为它的父母死了),如果您以 root 登录(其 pid =1 ),它将被 init 进程采用。
If you want child to exit first before parent then use wait() system call and its variants.
如果您希望孩子在父母之前先退出,请使用 wait() 系统调用及其变体。
回答by Rishabh Kumar
#include <stdio.h>
#include <unistd.h>
int main()
{
int pid,pid2;
pid=fork();
if (pid<0) {
printf("fork failed");
exit(-1);
} else if (pid==0) {
printf("child id is%d",getpid());
execlp("/bin/ls","is",NULL);
printf("\nsleeping for 2 seconds using pid of child class");
sleep(2);
printf("killing the child process");
kill(getpid());
} else {
wait(NULL);
printf("The parent id is %d\n",getpid());
printf("The child id is %d\n",getpid());
printf("\nsleeping for 3 seconds without pid");
sleep(3);
printf("\nchild completed\n");
exit(0);
}
}
回答by Naveen
It is simply, because the parent process no longer exists. If you call the wait()system function, then it will exist until the child finishes its work and you will get the parent PID.
很简单,因为父进程不再存在。如果你调用wait()系统函数,那么它会一直存在,直到孩子完成它的工作,你会得到父PID。

