开发者

Unix fork tree that only forks on one child

开发者 https://www.devze.com 2023-04-11 03:17 出处:网络
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

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

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

Example code

#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);
}


Why don't you use recursion instead of a for loop? Create a fork for the left side and call yourself for the right.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号