gpt4 book ai didi

c - 只在一个 child 上 fork 的 Unix fork 树

转载 作者:行者123 更新时间:2023-12-04 09:36:40 26 4
gpt4 key购买 nike

显然是家庭作业,但我并不是要任何人为我做,而是我只是想要方向。到目前为止,我已经把它写成一个 fork 进程树(这是一个很难弄清楚的问题)

   /* Includes */
#include <unistd.h> /* Symbolic Constants */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include<errno.h>

int main()
{
pid_t leftchild;
pid_t rightchild;
pid_t pid;
int level=0;
int max;


printf("Enter in max level for process tree: ");
scanf(" %d", &max);




pid=getpid();
fprintf(stderr,"I am the parent process with and id of: %ld\n", (long)getpid());
for(; level<max; level++){

leftchild=fork();
if (leftchild == -1)
{

fprintf(stderr, "can't fork, error %d\n", errno);
exit(EXIT_FAILURE);
}

if(leftchild==0){
fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());
continue;
}



else{
rightchild=fork();

if(rightchild==0){
fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());

continue;
}
}
wait(NULL);
wait(NULL);
break;

}

}

这个程序创建了这棵树

    i
/\
i i
/\ /\
i i i i

我需要创建另一个具有如下树的叉树:

     i
/ \
i i
/\
i i
/\
i i
/\
i i

我应该考虑对我的程序进行哪些修改?

我曾尝试在 rightchild if 语句中创建另一个分支,但没有成功。我什至尝试将所有东西分开,但失败得很惨。我只是没有从逻辑上看到解决方案。有什么提示或建议吗?

我试过递归:

     /* Includes */
#include <unistd.h> /* Symbolic Constants */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include<errno.h>
pid_t leftchild;
pid_t rightchild;
pid_t pid;
int level=0;
int max;



void recurse(){
if(level<2){
leftchild= fork();
if (leftchild == -1)
{

fprintf(stderr, "can't fork, error %d\n", errno);
exit(EXIT_FAILURE);
}

if(leftchild==0){
fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());

}

rightchild=fork();
if (rightchild == -1)
{

fprintf(stderr, "can't fork, error %d\n", errno);
exit(EXIT_FAILURE);
}

if(rightchild==0){
fprintf(stderr,"Level is %d, i am %ld , my parent is %ld\n",level, (long)getpid(), (long)getppid());

}
level++;
recurse();
}

}




int main()
{



printf("Enter in max level for process tree: ");
scanf(" %d", &max);


pid=getpid();
fprintf(stderr,"I am the parent process with and id of: %ld\n", (long)getpid());

recurse();



}

这实际上不会返回任何东西的 pid,有什么特别的原因吗?

最佳答案

你的左 child 不应该继续循环;它应该跳出循环,或者退出。它没有子项,但这仅仅意味着 wait() 调用每次都会返回一个错误。

你的右 child 应该继续循环以继续这个过程直到最后一层。

每一级的父进程应该在启动第二个(右边的)子进程后跳出循环,等待它的子进程结束。


例子

这似乎对我有用;这是对 SO 7624325 答案的简单改编,你之前的相关问题,尽管我从上面的示例代码中提取出来。

示例输出

I am the parent process with and id of: 13277
Level is 0, I am Left child 13278, my parent is 13277
Exiting: 13278
Level is 0, I am Right child 13279, my parent is 13277
Level is 1, I am Left child 13280, my parent is 13279
Level is 1, I am Right child 13281, my parent is 13279
Exiting: 13280
Level is 2, I am Right child 13283, my parent is 13281
Level is 2, I am Left child 13282, my parent is 13281
Exiting: 13282
Level is 3, I am Left child 13284, my parent is 13283
Level is 3, I am Right child 13285, my parent is 13283
Exiting: 13284
Level is 4, I am Left child 13286, my parent is 13285
Exiting: 13286
Level is 4, I am Right child 13287, my parent is 13285
Level is 5, I am Left child 13288, my parent is 13287
Exiting: 13288
Level is 5, I am Right child 13289, my parent is 13287
Level is 6, I am Right child 13291, my parent is 13289
Level is 6, I am Left child 13290, my parent is 13289
Exiting: 13290
Exiting: 13291
Exiting: 13289
Exiting: 13287
Exiting: 13285
Exiting: 13283
Exiting: 13281
Exiting: 13279
Exiting: 13277

示例代码

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

static void run_process(const char *child, int level)
{
fprintf(stderr, "Level is %d, I am %-5s child %ld, my parent is %ld\n",
level, child, (long)getpid(), (long)getppid());
}

int main(void)
{
int max = 7;

fprintf(stderr, "I am the parent process with and id of: %ld\n", (long)getpid());

for (int level = 0; level < max; level++)
{
pid_t leftchild;
pid_t rightchild;
if ((leftchild = fork()) < 0)
{
fprintf(stderr, "can't fork, error %d\n", errno);
exit(EXIT_FAILURE);
}
else if (leftchild == 0)
{
run_process("Left", level);
break;
}
else if ((rightchild = fork()) < 0)
{
fprintf(stderr, "can't fork, error %d\n", errno);
exit(EXIT_FAILURE);
}
else if (rightchild == 0)
{
run_process("Right", level);
continue;
}
else
break;
}

wait(NULL);
wait(NULL);
fprintf(stderr, "Exiting: %ld\n", (long)getpid());

return(0);
}

关于c - 只在一个 child 上 fork 的 Unix fork 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7682988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com