c++ - 为什么这段代码会失败? child 不等

标签 c++ ubuntu exec parent-child waitpid

好吧,我一直在努力学习掌握子进程并正确等待它们完成。我已经阅读了很多 Stack Overflow Q/A,但我似乎仍然无法让它按照我想要的方式工作。我一直在阅读/搜索这本书(C++ Primer 加第 6 版) - 我已经做了一些研究,但我仍然不能让它按我想要的方式等待。

所以我在 Stack Overflow 上查看了一些内容以供引用:Checking the status of a child process in C++

这是我迄今为止尝试过的:

using namespace std;
int main() {
int status;
pid_t child;
child = fork();

if ( child > 0 ) {
    cout << "\nChild #1 is in charge\n";
    execlp("ls", "ls", NULL);
}

else if ( child < 0 ) {
    cout << "\nSomething wen't wrong in the forking process\n";
}

else {

}

child = fork();
if ( child > 0 ) {
    cout << "\nSecond child is in charge\n";
    execlp("locate", "locate", "etc", NULL);
}

else if ( child < 0 ) {
    cout << "\nSomething went wrong in the forking of second child!\n";
}

else {

}

现在这将显示 child #1负责第二个 child 负责然后它会混合这两个命令(我看到一些ls code> 之间定位 etc)。

我尝试过的第二件事:

using namespace std;
int main() {
int status;
pid_t child;
pid_t ch_status = waitpid(child, &status, WNOHANG);

child = fork();
if ( child > 0 ) {
    cout << "\nChild is in charge\n";
    execlp("ls", "ls", NULL);
}

else if ( child < 0 ) {
    cout << "\nSomething wen't wrong in the forking process\n";
}

if ( ch_status == 0 ) {

}

else if ( ch_status == -1 ) {
    cout << "\nERROR IN CHILD #1\n";
}

else {

}

child = fork();
if ( child == 0 ) {
    cout << "\nSecond child is in charge\n";
    execlp("locate", "locate", "etc", NULL);
}

else if ( child < 0 ) {
    cout << "\nSomething went wrong in the forking of second child!\n";
}

if ( ch_status == 0 ) {

}

else if ( ch_status == -1 ) {
    cout << "\nERROR IN CHILD #1\n";
}

else {

}

child = fork();
if ( child > 0 ) {
    cout << "\nThird child is in charge!\n";
    execlp("echo", "echo", "herro", NULL);
}

else if ( child < 0 ) {
    cout << "\nForking of third child failed!\n";
}

if ( ch_status == 0 ) {

}

else if ( ch_status == -1 ) {
    cout << "\nERROR IN CHILD #2\n";
}

else {

}

return 0;
}

这更多地基于我提供的链接,它产生了与我第一次测试运行相同的结果,除了它还会显示ERROR IN CHILD #1/2

这些命令是无关紧要的,我只是不想明白我在这里做错了什么...我也尝试将它们嵌套在 else {//start secondary fork here } 中,但是我这也无济于事。

根据我阅读waitpid(2)手册后的理解,我应该使用WNOHANGhttp://linux.die.net/man/2/waitpid

非常感谢任何建议/指示。

如果可能,请提交示例代码,说明如何实现正确的结果(执行命令 1 -> 等待完成 -> 执行命令 2 -> 退出)

期待回复。

最佳答案

您应该在 fork 后在父级中调用waitpid()。也就是说,它应该进入 child > 0 分支。

该函数的目的是“等待子状态的变化”。但是,您在生成子级之前调用它。

代码应该如下所示:

using namespace std;
int main() {

int status;
pid_t child;

child = fork();
if ( child == 0 ) {
    cout << "\nChild is in charge" << endl;
    execlp("ls", "ls", NULL);
} else if ( child < 0 ) {
    cout << "\nSomething wen't wrong in the forking process" << endl;
} else {
    cout << "Parent waiting" << endl;
    pid_t ch_status = waitpid(child, &status, WNOHANG);
    if (ch_status == -1) {
      cout << "\nERROR IN CHILD #1" << endl;
    }
}

child = fork();
//same procedure as above

}

关于c++ - 为什么这段代码会失败? child 不等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16110432/

相关文章:

c++ - vc++ 2010/2012:包含 unique_ptr 的结构的 std::vector 编译器错误

c++ - 3D 金字塔与 OPENGL

c++ - 使用 Qpr​​ocess 或系统调用 R 时如何获取输出

c++ - 使用 clang 选择特定的 libstdc++ 版本

PHP - Linux 上的 Ping exec 挂起脚本

c++ - 通过索引 : how to? 的子数组

C++ 具有返回值的匿名函数

python - 加载 csv 文件时出现内存错误?

java - 带有 java Runtime.getRuntime().exec(command) 的 mysqldump 命令不生成转储

java - 使用从命令行运行的命令在 Java 中使用 exec 进行编译和执行失败