fork() and exec() Two Child Processes

半腔热情 提交于 2019-12-23 02:52:34

问题


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;
}

回答1:


else if(cpid_a == 0) { // code for child process A
    execlp("/bin/ls", "ls", NULL);

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.

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.

Now, the parent process goes ahead and fork() a second time, proceeded on the rest of your plan.

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.



来源:https://stackoverflow.com/questions/42685719/fork-and-exec-two-child-processes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!