C语言 fork() 和 wait() 带有两个子进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2708477/
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
fork() and wait() with two child processes
提问by Joe
I need to use the fork() and wait() functions to complete an assignment. We are modelling non-deterministic behaviour and need the program to fork() if there is more than one possible transition.
我需要使用 fork() 和 wait() 函数来完成任务。我们正在对非确定性行为进行建模,如果存在多个可能的转换,则需要程序进行 fork()。
In order to try and work out how fork and wait work, I have just made a simple program. I think I understand now how the calls work and would be fine if the program only branched once because the parent process could use the exit status from the single child process to determine whether the child process reached the accept state or not.
为了尝试弄清楚 fork 和 wait 是如何工作的,我刚刚编写了一个简单的程序。我想我现在明白调用是如何工作的,如果程序只分支一次就可以了,因为父进程可以使用单个子进程的退出状态来确定子进程是否达到接受状态。
As you can see from the code that follows though, I want to be able to handle situations where there must be more than one child processes. My problem is that you seem to only be able to set the status using an _exit function once. So, as in my example the exit status that the parent process tests for shows that the first child process issued 0 as it's exit status, but has no information on the second child process.
但是,正如您从后面的代码中看到的那样,我希望能够处理必须有多个子进程的情况。我的问题是您似乎只能使用 _exit 函数设置一次状态。因此,在我的示例中,父进程测试的退出状态显示第一个子进程发出 0 作为退出状态,但没有关于第二个子进程的信息。
I tried simply not _exit()-ing on a reject, but then that child process would carry on, and in effect there would seem to be two parent processes.
我只是尝试在拒绝时不使用 _exit()-ing,但是该子进程将继续执行,实际上似乎有两个父进程。
Sorry for the waffle, but I would be grateful if someone could tell me how my parent process could obtain the status information on more than one child process, or I would be happy for the parent process to only notice accept status's from the child processes, but in that case I would successfully need to exit from the child processes which have a reject status.
对不起,华夫饼,但如果有人能告诉我我的父进程如何获取多个子进程的状态信息,我将不胜感激,或者我很高兴父进程只通知子进程的接受状态,但在这种情况下,我需要成功退出具有拒绝状态的子进程。
My test code is as follows:
我的测试代码如下:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main(void) {
pid_t child_pid, wpid, pid;
int status = 0;
int i;
int a[3] = {1, 2, 1};
for(i = 1; i < 3; i++) {
printf("i = %d\n", i);
pid = getpid();
printf("pid after i = %d\n", pid);
if((child_pid = fork()) == 0) {
printf("In child process\n");
pid = getpid();
printf("pid in child process is %d\n", pid);
/* Is a child process */
if(a[i] < 2) {
printf("Should be accept\n");
_exit(1);
} else {
printf("Should be reject\n");
_exit(0);
}
}
}
if(child_pid > 0) {
/* Is the parent process */
pid = getpid();
printf("parent_pid = %d\n", pid);
wpid = wait(&status);
if(wpid != -1) {
printf("Child's exit status was %d\n", status);
if(status > 0) {
printf("Accept\n");
} else {
printf("Complete parent process\n");
if(a[0] < 2) {
printf("Accept\n");
} else {
printf("Reject\n");
}
}
}
}
return 0;
}
回答by Jonathan Leffler
It looks to me as though the basic problem is that you have one wait()call rather than a loop that waits until there are no more children. You also only wait if the last fork()is successful rather than if at least one fork()is successful.
在我看来,基本问题似乎是您有一个wait()调用,而不是一个循环,直到没有更多的孩子。您也只能等待最后一个fork()成功,而不是至少一个fork()成功。
You should only use _exit()if you don't want normal cleanup operations - such as flushing open file streams including stdout. There are occasions to use _exit(); this is not one of them. (In this example, you could also, of course, simply have the children return instead of calling exit()directly because returning from main()is equivalent to exiting with the returned status. However, most often you would be doing the forking and so on in a function other than main(), and then exit()is often appropriate.)
仅_exit()当您不希望进行正常的清理操作时才应使用- 例如刷新打开的文件流,包括stdout. 有场合使用_exit();这不是其中的一个。(在这个例子中,当然,你也可以简单地让孩子们返回而不是exit()直接调用,因为从返回main()相当于以返回的状态退出。但是,大多数情况下,你会在其他函数中执行分叉等操作than main(),然后exit()通常是合适的。)
Hacked, simplified version of your code that gives the diagnostics I'd want. Note that your forloop skipped the first element of the array (mine doesn't).
提供我想要的诊断的代码的简化版本。请注意,您的for循环跳过了数组的第一个元素(我的没有)。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(void)
{
pid_t child_pid, wpid;
int status = 0;
int i;
int a[3] = {1, 2, 1};
printf("parent_pid = %d\n", getpid());
for (i = 0; i < 3; i++)
{
printf("i = %d\n", i);
if ((child_pid = fork()) == 0)
{
printf("In child process (pid = %d)\n", getpid());
if (a[i] < 2)
{
printf("Should be accept\n");
exit(1);
}
else
{
printf("Should be reject\n");
exit(0);
}
/*NOTREACHED*/
}
}
while ((wpid = wait(&status)) > 0)
{
printf("Exit status of %d was %d (%s)\n", (int)wpid, status,
(status > 0) ? "accept" : "reject");
}
return 0;
}
Example output (MacOS X 10.6.3):
示例输出(MacOS X 10.6.3):
parent_pid = 15820
i = 0
i = 1
In child process (pid = 15821)
Should be accept
i = 2
In child process (pid = 15822)
Should be reject
In child process (pid = 15823)
Should be accept
Exit status of 15823 was 256 (accept)
Exit status of 15822 was 0 (reject)
Exit status of 15821 was 256 (accept)
回答by Richard Pennington
Put your wait() function in a loop and wait for all the child processes. The wait function will return -1 and errno will be equal to ECHILD if no more child processes are available.
将您的 wait() 函数放入循环中并等待所有子进程。如果没有更多子进程可用,wait 函数将返回 -1 并且 errno 将等于 ECHILD。
回答by Rupert Bailey
brilliant example Jonathan Leffler, to make your code work on SLES, I needed to add an additional header to allow the pid_t object :)
乔纳森莱夫勒的精彩例子,为了让你的代码在 SLES 上工作,我需要添加一个额外的头来允许 pid_t 对象:)
#include <sys/types.h>

