Question

In C programming language.

Problem 1 (Building a given Process Tree) 10 points) In this exercise you are asked to write a program which generates the following process tree (Scheme 1) Scheme 1: A given process tree The processes that you generate must remain active for some considerable window of time in order for the user to be able to observe the tree. The leaf process executes a call to: sleep() The internal process awaits of the termination of all its children processes. Every process printsa corresponding message every time it transitions to another phase (for example: start, loop to wait for children termination, allowing its own termination), so that the validation of the correct program operation is feasible. In order to separate the processes, please make sure that every process terminates with a different return code. In this example, one scenario can be At this point, you may find helpful a number of auxiliary functions for process handling, such as those that 1) have to do with identifying the circumstances under which a child process terminated (included in your ppt slides) 2) display the process tree starting from the root process (included in the appendix) 3) are related with different ways of recursively traversing a tree once the whole process tree is generated, etc

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The output snapshot is followed by the code. Explanatory comments are added.

umairaz@umairaz:~/CHEGGS gcc processtree.c umairaz@umairaz:-/CHEGG$ umairaz@umairaz:-/CHEGG$ umairaz@umairaz:~/CHEGGS ./a.out


#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

/* Structures to define the process tree */
struct processInfo
{
    char processName; /* A, B, C, D */
    int numChild;
    char children[5];
    int retValue;
};

/* Define the process tree */
struct processInfo process[] = {
    { 'A', 2, {'B', 'C'}, 5},
    { 'B', 1, {'D'}, 9 },
    { 'C', 0, { }, 8 },
    { 'D', 0, { }, 12 },
};

char myId = 'A'; /* my process id */
int retVal;

void processfunction()
{
    int i, j;
    int n = sizeof(process) / sizeof(struct processInfo);

    printf("Started process %c, pid=%d\n", myId, getpid());

    /* Get the children details */
    for(i = 0; i < n; i++)
    {
        if(myId == process[i].processName) /* process details found */
            break;
    }
    if(i < n) /* entry found */
    {
        /* update return value */
        retVal = process[i].retValue;
        /* create forked children */
        pid_t pids[5];
        for(j = 0; j < process[i].numChild; j++)
        {
            pids[j] = fork();
            if(pids[j] < 0)
            {
                printf("Process %c, pid=%d: fork failed\n", myId, getpid());
            }
            if(pids[j] == 0)
            {
                /* child process */
                /* 1. update id */
                myId = process[i].children[j];
                /* 2. call the processfunction, and return */
                processfunction();
                return;
            }
            else
            {
                printf("Process %c, pid=%d: Forked %c, pid=%d\n", myId, getpid(), process[i].children[j], pids[j]);
            }
        }
        /* Children forked, wait for children to end */
        printf("Process %c, pid=%d: Waiting for children to end\n", myId, getpid());
        for(j = 0; j < process[i].numChild; j++)
        {
            int status;
            if(pids[j] > 0) /* child was successfully forked */
            {
                waitpid(pids[j], &status, 0);
                printf("Process %c, pid=%d: Child exited with status %d\n", myId, getpid(), WEXITSTATUS(status));
            }
        }
    }

    /* Sleep for sometime */
    sleep(10);
    printf("Process %c, pid=%d: ending process\n", myId, getpid());
}

int main()
{
    processfunction();
    return retVal;
}

Add a comment
Know the answer?
Add Answer to:
In C programming language. Problem 1 (Building a given Process Tree) 10 points) In this exercise...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT