运行3个儿童过程
Running 3 child processes
我有三个子过程和一个父母。
我希望该程序以该顺序运行[child1 then child2 tht Child3然后parent]。
我一直在尝试使用follwing代码,但它不给我正确的顺序!
代码:
#include<iostream>
#include<string.h>
#include<fstream>
#include<cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main()
{
pid_t ch11;
pid_t ch22;
pid_t ch33;
int ch1 = fork();
int ch2 = fork();
int ch3 = fork();
if (ch1==0) //child1
{ cout<<"this is child 1nGoing from 1 to child 2 ...nn";
exit(0);
}
else if ( ch2==0)
{ waitpid(ch11,0,0);
cout<<"This is child 2nGoing from 2 To Child 3 ...nn";
exit(0);
}
else if (ch3==0)
{ waitpid(ch22,0,0);
cout<<"This is child 3nFineshed !! going from 3 to parentnn";
exit(0);
}
else
{ waitpid(ch33,0,0);
cout<<"This is parent , waited the whole childes to finish !!nn";
exit(0);
}
return 0 ;
}
输出:
ubuntu@ubuntu-desktop:~$ c++ a.cpp -o p1
ubuntu@ubuntu-desktop:~$ ./p1
This is child 2
Going from 2 To Child 3 ...
this is child 1
Going from 1 to child 2 ...
this is child 1
Going from 1 to child 2 ...
this is child 1
Going from 1 to child 2 ...
This is child 2
Going from 2 To Child 3 ...
This is child 3
Fineshed !! going from 3 to parent
this is child 1
Going from 1 to child 2 ...
This is parent , waited the whole childs to finish.
我知道它将通过使用waitpid()
函数来解决,我认为我使用waitpid()
错误。
这是您代码的更好的仪器版本;它在输出中包括PID,并且输出为一条线。
#include <iostream>
#include <cstdlib>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
int main()
{
int ch1 = fork();
int ch2 = fork();
int ch3 = fork();
if (ch1 == 0) // child1
{
cout << (int)getpid() << ": This is child 1 - Finishedn";
exit(0);
}
else if (ch2 == 0)
{
waitpid(ch1, 0, 0);
cout << (int)getpid() << ": This is child 2 - Finishedn";
exit(0);
}
else if (ch3 == 0)
{
waitpid(ch2, 0, 0);
cout << (int)getpid() << ": This is child 3 - Finished!n";
exit(0);
}
else
{
waitpid(ch3, 0, 0);
cout << (int)getpid() << ": This is parent - waited for all children to finish!n";
exit(0);
}
return 0;
}
样本输出:
$ ./3kids
40287: This is child 3 - Finished!
40285: This is child 1 - Finished
40286: This is child 2 - Finished
40290: This is child 1 - Finished
40289: This is child 2 - Finished
40288: This is child 1 - Finished
40284: This is parent - waited for all children to finish!
40291: This is child 1 - Finished
$
您可以看到,有一个过程认为自己是孩子3,两个过程认为自己是孩子2,而四个认为自己是孩子1的过程和一个认为自己是父母的过程。这与创建8个过程的不受约束的分叉一致。
只有3个孩子,要等待每个孩子,您需要更多的代码:
#include <iostream>
#include <cstdlib>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
void child(int n)
{
flush(cout); // No pending output
int pid = fork();
if (pid < 0)
cerr << (int)getpid() << ": failed to forkn";
else if (pid == 0)
{
cout << (int)getpid() << ": This is child " << n << " - Finishedn";
exit(0);
}
else
{
int corpse;
int status;
while ((corpse = wait(&status)) != -1)
cout << (int)getpid() << ": PID " << corpse << " exited with status "
<< status << "n";
}
}
int main()
{
child(1);
child(2);
child(3);
cout << (int)getpid() << ": This is parent - waited for all children to finish!n";
return 0;
}
样本输出:
$ ./3kids
40336: This is child 1 - Finished
40335: PID 40336 exited with status 0
40337: This is child 2 - Finished
40335: PID 40337 exited with status 0
40338: This is child 3 - Finished
40335: PID 40338 exited with status 0
40335: This is parent - waited for all children to finish!
$
尽管您的评论"代码正在起作用,但它给了我错误的执行顺序。因此,我将在这里尝试。
int main()
{
pid_t ch11;
pid_t ch22;
pid_t ch33;
这些变量的目的是什么?
int ch1 = fork();
您阅读了叉式手册吗?它清楚地指出,何时创建子过程,它将在子女中返回0,并在父母中返回PID。它也可能失败,因此您应该检查过。
考虑到这一点....
int ch2 = fork();
孩子和父都到达此行。因此,以前创建的儿童 forks以及。
int ch3 = fork();
猜猜是什么。
if (ch1==0) //child1
{ cout<<"this is child 1nGoing from 1 to child 2 ...nn";
除非您不是(见下文)。
exit(0);
应该是_exit。
}
else if ( ch2==0)
{ waitpid(ch11,0,0);
什么?CH11甚至都不是初始化的,那么这应该做什么?您是否启用了警告来编译代码?
一般而言,我不知道您是否想制作 rickid 叉,从而创建了与父母关系的过程链,或者您想要同一父母的3个孩子过程。如果后者这是不好的,那么,如果前者这是更不错的,因为您将无法等待这样的过程。
[snip the rest]
最后,让我们地址
执行的错误序列
什么?
无法保证您的流程执行顺序。实际部
鉴于到目前为止所显示的内容,我倾向于猜测您要么误解了您的作业作业,要么"不常见"。无论哪种方式,我都强烈建议您说明应该用您的代码解决的实际问题。
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 递归函数计算序列中的平方和(并输出过程)
- 我应该如何分享该过程和多个DLL之间的功能列表
- 尝试分叉一个过程,第一个命令有效,但第二总是给出错误
- 3个过程的严格交替
- 使用两个不同的管道C 读取和写入相同的过程
- 为什么两个执行矩阵乘法的过程并行运行比连续运行慢
- 在截断过程中乘以 2 个数字的问题
- C 中的远程过程调用(RPC):在硬编码端点时,可以多个客户端侦听一台服务器
- C++中可以通过多少个循环完成以下过程
- 运行3个儿童过程
- 从主过程执行多个过程
- 是否可以在同一过程中具有多个ORB对象
- 河豚加密在加密和解密过程中弄乱了前 8 个字节
- QProcess启动过程(搅拌机.exe)具有多个参数
- 我是否可以使用单个 SetSubclassWindow 过程来子类化多个编辑控件,如果可以,如何做到这一点
- 在一行中定义多个变量,这能为构建过程提供任何优化吗?