Unix环境下C++二叉fork算法生成9个子进程的问题排查
类Unix系统中fork二叉生成恰好9个子进程的问题
我正在学习类Unix操作系统中fork的工作机制,希望通过二叉fork算法(每个父进程仅能创建2个子进程)生成恰好9个子进程。但我无法在第三轮递归调用中正确限制子进程的创建数量:当前代码第一轮生成2个子进程,第二轮生成4个,第三轮生成了4个,而预期应为3个。以下是子进程创建流程图、现有代码及运行输出:
[子进程创建流程图]
#include <iostream.h> #include <sys/wait.h> #include <unistd.h> #include <stdlib.h> using namespace std; static int CHILD_SIZE = 9; static int LEVEL = 1; static int MAX_LEVEL = 3; void createChildren(int remaining, int level) { int CURRENT_REMAINING = remaining; if(CURRENT_REMAINING <= 0 || level > MAX_LEVEL){ return; } pid_t firstChild,secondChild; // Fork the first child firstChild = fork(); if(firstChild == -1){ cerr << "Fork failed!" << endl; } else if (firstChild == 0 && CURRENT_REMAINING > 1) { // first child cout << "First child process (PID: " << getpid() << ") with parent process (PID: " << getppid() << ") at level: " << level << endl; // calculate how many child needs after this iteration CURRENT_REMAINING -= (2 * level); if(CURRENT_REMAINING < 0){ CURRENT_REMAINING = 0; } } else if (firstChild > 0 && CURRENT_REMAINING > 1) { // Fork the second child secondChild = fork(); if(firstChild == -1){ cerr << "Fork failed!" << endl; } else if (secondChild == 0) { // second child waitpid(firstChild, NULL, 0); cout << "Second child process (PID: " << getpid() << ") with parent process (PID: " << getppid() << ") at level: " << level << endl; // calculate how many child needs after this iteration CURRENT_REMAINING -= (2 * level); if(CURRENT_REMAINING < 0){ CURRENT_REMAINING = 0; } } else if (secondChild > 0){ // Wait for both child processes to complete waitpid(firstChild, NULL, 0); waitpid(secondChild, NULL, 0); } } //---------- recursion call-----------// // upper child recursion call if(firstChild == 0){ cout << "current remaining in first child is: " << CURRENT_REMAINING << " at level " << level << endl; createChildren(CURRENT_REMAINING, level + 1); } // lower child recursion call if(secondChild == 0){ cout << "current remaining in second child is: " << CURRENT_REMAINING << " at level " << level << endl; if(CURRENT_REMAINING > 3){ //cout << "Create two more processes" << endl; createChildren(CURRENT_REMAINING, level + 1); } } } int main() { std::cout << "Parent process " << getpid() << std::endl; createChildren(CHILD_SIZE, LEVEL); return 0; }
运行输出
Parent process 8571 First child process (PID: 8573) with parent process (PID: 8571) at level: 1 current remaining in first child is: 7 at level 1 Second child process (PID: 8574) with parent process (PID: 8571) at level: 1 current remaining in second child is: 7 at level 1 First child process (PID: 8575) with parent process (PID: 8573) at level: 2 current remaining in first child is: 3 at level 2 First child process (PID: 8576) with parent process (PID: 8574) at level: 2 current remaining in first child is: 3 at level 2 Second child process (PID: 8577) with parent process (PID: 8573) at level: 2 current remaining in second child is: 3 at level 2 Second child process (PID: 8578) with parent process (PID: 8574) at level: 2 current remaining in second child is: 3 at level 2 First child process (PID: 8579) with parent process (PID: 8575) at level: 3 current remaining in first child is: 0 at level 3 Second child process (PID: 8581) with parent process (PID: 8575) at level: 3 current remaining in second child is: 0 at level 3 First child process (PID: 8580) with parent process (PID: 8576) at level: 3 current remaining in first child is: 0 at level 3 Second child process (PID: 8582) with parent process (PID: 8576) at level: 3 current remaining in second child is: 0 at level 3
内容的提问来源于stack exchange,提问作者djflvv233
相关产品推荐
相关产品推荐

