fork()和exec()两个子进程 [英] fork() and exec() Two Child Processes
问题描述
我两次调用fork()来创建两个子进程.我希望子进程A进行exec()调用,并且希望子进程B也进行exec()调用.给定代码的问题是,子进程A中的第一个exec()之后,下一个fork()似乎没有发生,程序退出了.我认为这与exec()如何覆盖父进程有关.我要完成的是从fork()创建的每个子进程中调用exec().
I am calling fork() twice to create two child processes. I want child process A to do an exec() call and child process B to also do an exec() call. The problem I am having with the given code is that after the first exec() from child process A, the next fork() does not seem to occur and the program exits. I think that it has to do with how exec() overlays the parent process. What I want to accomplish is to call exec() from each of the child processes created by fork().
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
int main() {
pid_t cpid_a, cpid_b;
cpid_a = fork();
if(cpid_a < 0) {
std::cout << "Fork failed." << '\n';
return 1;
}
else if(cpid_a == 0) { // code for child process A
execlp("/bin/ls", "ls", NULL);
cpid_b = fork();
if(cpid_b < 0) {
std::cout << "Fork failed." << '\n';
return 1;
}
else if(cpid_b == 0) { // code for child process B
execlp("/bin/ls", "ls", NULL);
}
}
else { // code for parent process
while(wait(NULL) != -1);
}
return 0;
}
推荐答案
else if(cpid_a == 0) { // code for child process A
execlp("/bin/ls", "ls", NULL);
如果此调用成功,则执行以下语句,之后将不执行任何操作.这就是exec()
的工作方式.紧随其后的fork()
永远不会发生.这就是exec()
的工作方式.如果exec()
成功,它将永不返回.替换过程将在其位置执行.
If this calls succeeds, the following statement, and nothing that follows will ever be executed. That's how exec()
works. The immediately-following fork()
never occurs. That's simply how exec()
works. If exec()
succeeds, it never returns. The replacement process gets executed in its place.
您甚至在上面添加了100%正确的注释:子进程A的代码". if()
语句中的所有内容均为子进程A的代码",并在fork()
返回0时执行.
You even added the 100% correct comment, above: "code for child process A". Everything inside the if()
statement is "code for child process A", and gets executed when fork()
returns 0.
您还正确地声明了您希望父进程派生第二个进程.好吧,您需要让 代码明显地由父进程而不是子进程执行:
You also correctly stated that you want the parent process to fork a second process. Well, you need to have that code obviously get executed by the parent process, and not the child process:
else if(cpid_a == 0) { // code for child process A
execlp("/bin/ls", "ls", NULL);
exit(1);
} else {
cpid_b = fork();
// The rest of the code.
现在,父流程继续进行,第二遍fork()
,继续执行您的其余计划.
Now, the parent process goes ahead and fork()
a second time, proceeded on the rest of your plan.
P.S. exit()
只是一个很好的衡量标准. exec()
返回的唯一时间是exec()
无法执行给定进程时. /bin/ls
的可能性很小;如果丢失了,您还有更大的问题要担心.尽管如此,这在技术上是正确的,因为在那一点上继续执行将导致完全混乱.同样,如果缺少/bin/ls
,那将是最少的问题,但是,例如,如果系统用完了内存并且由于该原因无法执行它,也可能会发生这种情况.在这种情况下,无需向火上添加燃料;但无论如何,进程都会死掉.
P.S. The exit()
is just for a good measure. The only time exec()
returns is when exec()
fails to execute the given process. Highly unlikely, in the case of /bin/ls
; if it's missing you have bigger problems to worry about. Still, that's the technically correct thing to do, since continuing execution at that point will result in complete chaos. Again, if /bin/ls
is missing that's going to be the least of the problems, but this can also happen if, say, the system ran out of memory and can't execute it for that reason; in which case there's no need to add fuel to the fire; but rather have the process die anyway.
这篇关于fork()和exec()两个子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!