Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6675995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:55:53+00:00 2026-05-26T03:55:53+00:00

Obviously homework, however I am not asking for anyone to do it for me

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:55:54+00:00Added an answer on May 26, 2026 at 3:55 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is homework, just so that's mentioned. I have project called AdventCalendar, which is
Obviously there are security reasons to close a wireless network and it's not fun
Obviously I can use BCP but here is the issue. If one of the
Obviously (methinks), creating an index on a BIT column is unnecessary. However, if you
Obviously it gets updated during a write operation, but are there any non-destructive operations
Obviously I can do and DateTime.Now.After - DateTime.Now.Before but there must be something more
I know you guys hate helping with homework, but I thought I might ask
As part of a homework assignment, I have to program a simple chess game
I have been given a homework assignment where I need to create a captcha
I have a homework assignment to emulate floating point casts, e.g.: int y =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.