不能 运行 在所有子进程终止后父进程。
cant run parent process after all child processes have been terminated.
我有一个程序,它在循环中创建多个 fork() 之后执行父进程之后的所有子进程。
但是在每个子进程终止之前,父进程是 运行。
im childprocess : 18389
parent process done
im childprocess : 18390
parent process done
im childprocess : 18391
parent process done
这是我如何使用 fork() 调用的代码
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);
}
}
}
}
在这里,您在循环的每次迭代中创建一个子进程,然后在同一迭代中等待它。因此,在一次迭代结束时,一个子进程被创建,它打印然后退出,父进程从等待中唤醒并打印,因此您得到前两行。
下一次迭代的输出类似,因此循环的每次迭代都会得到两行,看起来父项在子项之前执行,但事实并非如此。
如果您想在所有子进程完成后调用父进程,请执行以下操作。
引入一个全局变量isParent,如果当前进程是父进程则为真。将其初始化为零
int isParent = 0;
然后在循环中,而不是调用 parentProcess()
将 isParent
设置为 1
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
isParent = 1;
} else {
// fork failed
printf("fork() failed!\n");
return 1;
}
}
然后在 for 循环调用后 parentProcess
如果设置了 isParent
if(isParent){
ParentProcess(files_count)
}
然后在 parentProcess(int numChildren)
调用中等待所有子进程。
void parentProcess(int numChildren);
void parentProcess(int numChildren) {
while (true) {
int status;
int i;
for(i = 0;i < numChildren; i++){
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);
}
}
}
}
我有一个程序,它在循环中创建多个 fork() 之后执行父进程之后的所有子进程。 但是在每个子进程终止之前,父进程是 运行。
im childprocess : 18389
parent process done
im childprocess : 18390
parent process done
im childprocess : 18391
parent process done
这是我如何使用 fork() 调用的代码
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);
}
}
}
}
在这里,您在循环的每次迭代中创建一个子进程,然后在同一迭代中等待它。因此,在一次迭代结束时,一个子进程被创建,它打印然后退出,父进程从等待中唤醒并打印,因此您得到前两行。
下一次迭代的输出类似,因此循环的每次迭代都会得到两行,看起来父项在子项之前执行,但事实并非如此。
如果您想在所有子进程完成后调用父进程,请执行以下操作。
引入一个全局变量isParent,如果当前进程是父进程则为真。将其初始化为零
int isParent = 0;
然后在循环中,而不是调用 parentProcess()
将 isParent
设置为 1
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
isParent = 1;
} else {
// fork failed
printf("fork() failed!\n");
return 1;
}
}
然后在 for 循环调用后 parentProcess
如果设置了 isParent
if(isParent){
ParentProcess(files_count)
}
然后在 parentProcess(int numChildren)
调用中等待所有子进程。
void parentProcess(int numChildren);
void parentProcess(int numChildren) {
while (true) {
int status;
int i;
for(i = 0;i < numChildren; i++){
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);
}
}
}
}