Question: ***THIS NEEDS TO BE DONE IN ONLY C LANGUAGE ********** Assignment: Create process creation hierarchy as a dynamic array of length n which references the

***THIS NEEDS TO BE DONE IN ONLY C LANGUAGE **********

Assignment:

Create process creation hierarchy as a dynamic array of length n which references the process control blocks (PCBs), indexed 0 to n-1

Each PCB is a structure consisting of two fields:

  • parent: a PCB index corresponding to the process' creator
  • children: a pointer to a linked list, where each node contains the PCB index of one child process and a link to the next child in the list

The necessary functions are simplified as follows:

create() represents the create function, which prompts for the parent process PCB[p]. The function creates a new child process PCB[q] of process PCB[p] by performing the following tasks:

  • allocate a free PCB[q]
  • record the parent's index, p, in PCB[q]
  • initialize the list of children of PCB[q] as empty (NULL)
  • create new link containing the child's index q and append the link to the linked list of PCB[p]

destroy() represents the destroy function, which prompts for the parent process PCB[p]. The function recursively destroys all descendent processes (child, grandchild, etc.) of process PCB[p] by performing the following tasks: for each element q on the linked list of children of PCB[p]:

  • destroy(q) /* recursively destroy all descendants */
  • free PCB[q]
  • deallocate the element q from the linked list

**********THIS IS DEFAULT TEMPLATE NEED TO CODING NEXT TO EACH COMMENTS******

#include

#include

/* Define structures and global variables, including

linked list type And PCB type */

/***************************************************************/

void "OPTION 1"() {

/* declare local vars */

/* prompt for maximum number of processes */

/* allocate memory for dynamic array of PCBs */

/* Define PCB[0] */

/* Intitialize all other indices' parent to -1 */

return;

}

/***************************************************************/

void "OPTION 2"() {

/* define local vars */

/* prompt for parent process index */

/* search for next unused process q (parent=-1), using while loop */

/* allocate memory for new child PCB[q], set process index and initilize link to NULL */

/* record the parent's index p in PCB[q] */

/* initialize the list of children of PCB[q] as empty (NULL) */

/* create new link containing the child's index q and

appends the link to the end of the linked list of PCB[p] */

/* print message: cr[p] creates nth child of PCB[p] at PCB[q] */

return;

}

/***************************************************************/

void "OPTION #3--RECURSION PROCEDURE"(takes as parameter a variable of linked list type) {

/* declare local vars */

/* if node parameter passed is NULL, return */

/* else recursively call this procedure on node's link field */

/* set q to node's process field */

/* recursively call this procedure on pcb[q]'s children field */

/* print message of PCB[q] deleted */

/* free memory allocated by the node parameter */

/* set pcb[q]'s parent to -1 */

/* set node paramter to NULL */

return;

}

/***************************************************************/

void "OPTION #3"() {

/* delcare local vars */

/* prompt for parent process p */

/* print message of descendants to be destroyed */

/* call OPTION 3 recursive procedure on pcb[p]'s children */

/* set pcb[p]'s children to NULL */

return;

}

/***************************************************************/

void "OPTION #4"() {

/* if pcb is not NULL */

/* if pcb[0]'s children is not NULL */

/* call OPTION #3 recursive procedure on pcb[0]' children */

/* free memory allocated for pcb */

return;

}

/***************************************************************/

int main() {

/*declare local vars */

/* until user quits, print menu, prompt for user's choice, call appropriate procedure */

return 1;

}

Sample test run:

Process creation and destruction -------------------------------- 1) Enter parameters 2) Create a new child process 3) Destroy all descendants of a process 4) Quit program and free memory Enter selection: 1 Enter maximum number of processes: 5 Process creation and destruction -------------------------------- 1) Enter parameters 2) Create a new child process 3) Destroy all descendants of a process 4) Quit program and free memory Enter selection: 2 Enter the parent process index: 0 cr[0] /*creates 1st child of PCB[0] at PCB[1]*/ Process creation and destruction -------------------------------- 1) Enter parameters 2) Create a new child process 3) Destroy all descendants of a process 4) Quit program and free memory Enter selection: 2 Enter the parent process index: 0 cr[0] /*creates 2nd child of PCB[0] at PCB[2]*/ Process creation and destruction -------------------------------- 1) Enter parameters 2) Create a new child process 3) Destroy all descendants of a process 4) Quit program and free memory Enter selection: 2 Enter the parent process index: 2 cr[2] /*creates 1st child of PCB[2] at PCB[3]*/ Process creation and destruction -------------------------------- 1) Enter parameters 2) Create a new child process 3) Destroy all descendants of a process 4) Quit program and free memory Enter selection: 2 Enter the parent process index: 0 cr[0] /*creates 3rd child of PCB[0] at PCB[4]*/ Process creation and destruction -------------------------------- 1) Enter parameters 2) Create a new child process 3) Destroy all descendants of a process 4) Quit program and free memory Enter selection: 3 Enter the process whose descendants are to be destroyed: 0 de[0] /* destroys all descendants of PCB[0] which includes: PCB[4] PCB[3] PCB[2] PCB[1] */ Process creation and destruction -------------------------------- 1) Enter parameters 2) Create a new child process 3) Destroy all descendants of a process 4) Quit program and free memory Enter selection: 4 Quitting program

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!