0
我有一個程序,在循環中創建幾個fork()之後,在父進程之後執行所有子進程。 但相反,父進程在每個子進程終止之前運行。所有子進程終止後,無法運行父進程。
im childprocess : 18389
parent process done
im childprocess : 18390
parent process done
im childprocess : 18391
parent process done
這裏是我用叉子()的代碼調用
for (int file = 0; file < files_count; file++) {
pid_t pid = fork();
int file_loc = file + 2;
if (pid == 0) {
// child process
occurrences_in_file(argv[file_loc], argv[1]);
break;
} else if (pid > 0) {
// parent process
parentProcess();
} else {
// fork failed
printf("fork() failed!\n");
return 1;
}
}
。
void occurrences_in_file(const std::string& filename_,
const std::string& pattern_);
void occurrences_in_file(const std::string& filename_,
const std::string& pattern_) {
int my_pid;
cout << "im childprocess : " << my_pid <<endl;
}
。
void parentProcess();
void parentProcess() {
while (true) {
int status;
pid_t done = wait(&status);
if (done == -1) {
if (errno == ECHILD){
cout << "parent process done"<< endl;
break; // no more child processes
}
} else {
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
std::cerr << "pid " << done << " failed" << endl;
_exit(1);
}
}
}
}
我知道它在孩子之後執行。但我嘗試的是在所有childs之後執行它,以便輸出如下所示:im childprocess:18389 \ n im childprocess:18390 \ n im childprocess:18391 \ n父進程已完成... – LxSwiss
您編寫的代碼的作用是不這樣做。正如我所說,這段代碼在每次迭代中產生一個新的子代,並且父代等待那個子代完成,然後在下一次迭代中產生另一個子代。這也是爲什麼'父進程完成'被打印三次而不是一次。如果您想要在最後打印「完成父進程」,請按照我的編輯進行操作 – Nishant