Unix 分叉树只分叉一个孩子
显然是作业,但我并不是要求任何人为我做,而是我只是想要指导。到目前为止,我已经将其编写为分叉进程树(这是一个挑战),
/* 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
我应该考虑对我的程序进行哪些修改?
我尝试在右子 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,有什么特殊原因吗?
Obviously homework, however I am not asking for anyone to do it for me but rather I just want direction. So far I have already written this as a fork process tree(which was a challenge to figure out)
/* 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;
}
}
this program creates this tree
i
/\
i i
/\ /\
i i i i
I need to create another fork tree that have a tree like this:
i
/ \
i i
/\
i i
/\
i i
/\
i i
What modification should I look into making to my program?
I have tried creating a another fork inside the rightchild if statement and it didn't work. I even tried splitting everything up but that failed miserably. I am just not seeing the solution logically. any hints or suggestions?
I have tried recursion:
/* 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();
}
This actually wont return the pid for anything, any particular reason why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的左孩子不应该继续循环;它应该跳出循环或退出。它没有子级,但这仅仅意味着
wait()
调用每次都会返回错误。您的右孩子应该继续循环以继续该过程直到最后一层。
每个级别的父进程应在启动第二个(右侧)子进程后跳出循环,并等待其子进程终止。
示例
这似乎对我有用;这是对 SO 7624325 答案的简单改编,您之前的相关问题,尽管我是从上面的示例代码中提取出来的。
示例输出
示例代码
Your left child should not continue the loop; it should break out of the loop, or exit. It has no children, but that simply means the
wait()
call will return with an error each time.Your right child should continue the loop to continue the process down to the last level.
The parent process at each level should break out of the loop after launching the second (right) child, and wait for its children to die.
Example
This seems to work for me; it is a simple adaptation of the answer to SO 7624325, your previous related question, though I carved it out of your example code above.
Example output
Example code
为什么不使用递归而不是for循环呢?在左侧创建一个叉子,并在右侧调用自己。
Why don't you use recursion instead of a for loop? Create a fork for the left side and call yourself for the right.