我有三个子进程和一个父进程。
我希望程序以该顺序运行[Child1 then child2 then child3 then parent]。
我一直在尝试使用以下代码,但它没有给我正确的序列!
代码:
#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 1\nGoing from 1 to child 2 ...\n\n";
exit(0);
}
else if ( ch2==0)
{ waitpid(ch11,0,0);
cout<<"This is child 2\nGoing from 2 To Child 3 ...\n\n";
exit(0);
}
else if (ch3==0)
{ waitpid(ch22,0,0);
cout<<"This is child 3\nFineshed !! going from 3 to parent\n\n";
exit(0);
}
else
{ waitpid(ch33,0,0);
cout<<"This is parent , waited the whole childes to finish !!\n\n";
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()
错误。
答案 0 :(得分:1)
尽管您的评论“代码正在运行,但它给了我错误的执行顺序”但整个事情仍然完全不正确,显然你不明白其他人试图说什么,因此我会尝试这里。
int main()
{
pid_t ch11;
pid_t ch22;
pid_t ch33;
这些变量的目的是什么?
int ch1 = fork();
您是否阅读过fork手册页?它清楚地说明了何时创建子进程,它在子进程中返回0,在父进程中返回pid。它也可能失败,所以你应该检查一下。
考虑到这一点......
int ch2 = fork();
孩子和父母都会到达此行。因此,之前创建的子分叉。
int ch3 = fork();
猜猜是什么。
if (ch1==0) //child1
{ cout<<"this is child 1\nGoing from 1 to child 2 ...\n\n";
除非你不是(见下文)。
exit(0);
应该是_Exit。
}
else if ( ch2==0)
{ waitpid(ch11,0,0);
什么? ch11甚至没有初始化,那么这应该完成什么呢? 您是否在启用警告的情况下编译代码?
一般情况下,我不知道您是否正在尝试创建 children fork,从而创建一个包含父&lt; - &gt;子关系的进程链,或者您想要3个相同的子进程父进程。如果后者这只是坏事,如果前者更加错误,因为你无法等待这样的过程。
[snip the rest]
最后,让我们来谈谈
执行错误的序列
什么?
对流程执行顺序无任何保证。实际上,任何数量的它们都可以执行任何时间,并且在其他任何数量的机会运行之前可以多次安排。
鉴于到目前为止所显示的内容,我倾向于猜测你要么误解了你的家庭作业,要么非常“非常规”地接近它。无论哪种方式,我强烈建议您说明应该用您的代码解决的实际问题。
答案 1 :(得分:0)
这是代码的更好的检测版本;它包括输出中的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 - Finished\n";
exit(0);
}
else if (ch2 == 0)
{
waitpid(ch1, 0, 0);
cout << (int)getpid() << ": This is child 2 - Finished\n";
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 fork\n";
else if (pid == 0)
{
cout << (int)getpid() << ": This is child " << n << " - Finished\n";
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!
$