在所有子进程终止后,不能运行父进程
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,如果当前进程是父进程,该变量为true。初始化为0
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;
}
}
如果设置了isParent
,则在for循环之后调用parentProcess
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);
}
}
}
}
相关文章:
- 为什么我不能让 3 个网络摄像头与 pthreads 并行运行?
- C/C++ 不能在 for 循环中运行 for 循环
- 不能很好地运行HElib,但它建立在Windows 10 x64上
- Eclipse 中的 Makefile 项目可以运行,但不能调试,为什么?
- SImple 代码在 DevC++ 中正确运行,但不能在 Visual Studio Code 中正常运行
- 为什么 free 函数不能在 C 数组上运行,而 std::begin 在某些情况下可以在 C++14 中运行?
- 为什么我不能在另一台电脑上运行自己的dll(msvcrt.dll被错过)
- 我的代码是完整的,至少我相信它是...为什么它不能按预期运行?
- C++程序在Linux上完美运行,但不能在Windows上运行
- 为什么这个程序不能运行,但它会构建
- 为什么这段代码不能运行?[初级]
- 我的C++ Unity 插件在编辑器中运行一次,但不能运行两次
- 当使用CreateProcessLogonW或类似的方法启动时,进程启动,但不能运行
- 使用sublime-build时不能运行c++程序,从终端运行时工作正常
- 在所有子进程终止后,不能运行父进程
- 为什么我不能运行这个为什么这个c ++代码在Visual Studio中给出错误,尽管它在删除后在代码块中运行良好 #include "stdafx.h"
- 为什么我不能运行对象文件
- 一切都是正确的,但不能运行:错误预期{或;
- Eclipse可以编译,但不能运行
- 我的程序不能运行与计算机没有VS MSCR100D.dll丢失