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?
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