C 从孩子那里分出3个孙子的麻烦,顺序、PID和PPID不正确
您好,我有一个关于使用fork()创建更多子对象的问题,这个问题是基于我之前提出的一个问题 我希望我的输出看起来像这样(#s被简化,仅用于说明顺序) 但我的输出如下所示: 最后关于爸爸的ppid似乎还行,除了最后一个,大多数PID似乎都出了问题。我不知道为什么会有一个儿子,然后是5个,然后是3个儿子。这是我的密码:C 从孩子那里分出3个孙子的麻烦,顺序、PID和PPID不正确,c,linux,unix,fork,C,Linux,Unix,Fork,您好,我有一个关于使用fork()创建更多子对象的问题,这个问题是基于我之前提出的一个问题 我希望我的输出看起来像这样(#s被简化,仅用于说明顺序) 但我的输出如下所示: 最后关于爸爸的ppid似乎还行,除了最后一个,大多数PID似乎都出了问题。我不知道为什么会有一个儿子,然后是5个,然后是3个儿子。这是我的密码: int grandforking(null) { Gen1 (null); return 0; } int Gen1 (null) {
int grandforking(null)
{
Gen1 (null);
return 0;
}
int Gen1 (null)
{
void about(char *);
int i=0;
int j=0;
about("grandpa");
for(i = 0; i < 3; i++ )
{
pid_t child = 0;
child = fork();
if (child < 0)
{ //unable to fork error
perror ("Unable to fork");
exit(-1);
}
else if (child == 0)
{ //child process
Gen2 (null);
exit(0);
}
else
{ //parent process
//(do nothing)
}
}
for(j = 0; j < 3; j++ )
{
wait(NULL);//wait for parent to acknowledge child process
}
return 0;
}
int Gen2 (null)
{
int i=0;
int j=0;
about("dad");
for(i = 0; i < 3; i++ )
{
pid_t child = 0;
child = fork();
if (child < 0)
{ //unable to fork error
perror ("Unable to fork");
exit(-1);
}
else if (child == 0)
{ //child process
about ("son");
exit(0);
}
else
{ //parent process
//(do nothing)
}
}
for(j = 0; j < 3; j++ )
{
wait(NULL);//wait for parent to acknowledge child process
}
return 0;
}
int-grandforking(空)
{
第1代(空);
返回0;
}
int第1代(空)
{
空约(字符*);
int i=0;
int j=0;
关于(“爷爷”);
对于(i=0;i<3;i++)
{
pid_t child=0;
child=fork();
if(子级<0)
{//无法分叉错误
佩罗尔(“无法叉叉”);
出口(-1);
}
else if(子项==0)
{//子进程
第2代(空);
出口(0);
}
其他的
{//父进程
//(什么也不做)
}
}
对于(j=0;j<3;j++)
{
wait(NULL);//等待父进程确认子进程
}
返回0;
}
int第2代(空)
{
int i=0;
int j=0;
关于(“爸爸”);
对于(i=0;i<3;i++)
{
pid_t child=0;
child=fork();
if(子级<0)
{//无法分叉错误
佩罗尔(“无法叉叉”);
出口(-1);
}
else if(子项==0)
{//子进程
关于(“儿子”);
出口(0);
}
其他的
{//父进程
//(什么也不做)
}
}
对于(j=0;j<3;j++)
{
wait(NULL);//等待父进程确认子进程
}
返回0;
}
进程启动后,调度程序可以并将按照其希望的任何顺序运行它们。如果您有多个处理器,这包括同时运行多个处理器。(像现在的每个人一样。)
它当然可以启动一个子系统,运行该子系统一段时间,然后返回父系统打印消息
如果在父对象生成其子对象之前具有标识printf,则排序会稍微好一些
但获得lockstep订单的唯一方法是执行以下操作:
- 自认
- 循环起点
- 生孩子
- 等待孩子完成
- 在所有子项完成后退出
(父:)
,并根据需要缩进以指示父/子关系并显示流程树。好的,谢谢大家。如果有人对如何更好地格式化输出有任何想法(有点像Arlie所说的),我想现在就让它保持原样。你认为祖父母>>>父母>>>孙辈的实际分组是正确的,还是仅仅因为他们没有以正确的顺序显示在屏幕上?从你所说的听上去像是后者
int grandforking(null)
{
Gen1 (null);
return 0;
}
int Gen1 (null)
{
void about(char *);
int i=0;
int j=0;
about("grandpa");
for(i = 0; i < 3; i++ )
{
pid_t child = 0;
child = fork();
if (child < 0)
{ //unable to fork error
perror ("Unable to fork");
exit(-1);
}
else if (child == 0)
{ //child process
Gen2 (null);
exit(0);
}
else
{ //parent process
//(do nothing)
}
}
for(j = 0; j < 3; j++ )
{
wait(NULL);//wait for parent to acknowledge child process
}
return 0;
}
int Gen2 (null)
{
int i=0;
int j=0;
about("dad");
for(i = 0; i < 3; i++ )
{
pid_t child = 0;
child = fork();
if (child < 0)
{ //unable to fork error
perror ("Unable to fork");
exit(-1);
}
else if (child == 0)
{ //child process
about ("son");
exit(0);
}
else
{ //parent process
//(do nothing)
}
}
for(j = 0; j < 3; j++ )
{
wait(NULL);//wait for parent to acknowledge child process
}
return 0;
}