Question
#include #include /* Define structures and global variables */ struct node 1 { int process; struct node1 * link; }; typedef strucut node1 linked_list_type; struct
#include
/* Define structures and global variables */ struct node 1 { int process; struct node1 * link; };
typedef strucut node1 linked_list_type;
struct node2 { int parent; linked_list_type *children; } *pcb = NULL;
void parameter(){ /* declare local variables */ /* Procedure to print Hierarchy of processes */
};
void procedure_1(){ /* PROCEDURE FOR OPTION 1 */ /* declare local vars */ /* prompt for maximum number of processes */ /* allocate memory for dynamic array of PCBs */ /* Define PCB[0] */ /* for-loop to intitialize all other indices' parent to -1 */
return; }
void procedure_2(){ /* PROCEDURE FOR OPTION #2 */ /* define local vars */ /* prompt for parent process index p */ /* search for first available index q without a parent in a while loop */ /* allocate memory for new child process, initilize fields */ /* record the parent's index p in PCB[q] */ /* initialize the list of children of PCB[q] as empty */ /* create a new link containing the child's index q and append the link to the end of the linked list of PCB[p] */ /* call procedure to print current hierachy of processes */
return; }
void destroy(parameter){ /* RECURSIVE PROCEDURE TO DESTROY CHILDREN PROCESSES */ /* declare local vars */ /* check if end of linked list--if so return */ /* else call self on next node in linked list */ /* set variable q to current node's process index field */ /* call self on children of PCB[q] */ /* free memory of paramter */ /* reset parent of PCB[q] to -1 */ /* set paramter to NULL */
} /* end of else */ return; }
void procedure_3(){ /* PROCEDURE FOR OPTION #3 */ /* declare local vars */ /* prompt for process index p */ /* call recursive procedure to destroy children of PCB[p] */ /* reset children of PCB[p] to NULL */ /* call procedure to print current hierarchy of processes */
return; }
void procedure_4(){ /* PROCEDURE FOR OPTION #4 */ /* check if PCB is non null) /* check if children of PCB[0] is not null */ /* call recursive procedure to destroy children of PCB[0] */
} /* if */ /* free memory of PCB */ } /* if */ return; }
int main() { /* declare local vars */ /* while user has not chosen to quit */ /* print menu of options */ /* prompt for menu selection */ /* call appropriate procedure based on choice--use switch statement or series of if, else if, else statements */
} /* while loop */
return 1; /* indicates success */ }
////////////////////////////////////////////
Sample output 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 PCB[0] is the parent of: 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 PCB[0] is the parent of: PCB[1] 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 PCB[0] is the parent of: PCB[1] PCB[2] PCB[2] is the parent of: 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 PCB[0] is the parent of: PCB[1] PCB[2] PCB[4] PCB[2] is the parent of: 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: 3 Enter the index of the process whose descendants are to be destroyed: 0 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...
Assignment \#1--Process Creation Hierarchy Objective: To simulate process creation and destruction when implemented with linked lists. Specification: The program creates/destroys child processes based on choosing from a menu of choices, where each choice calls the appropriate procedure, where the choices are: 1) Enter parameters 2) Create a new child process 3) Destroy all descendants of a process 4) Quit program and free memory Assignment: - Create a process creation hierarchy as a dynamic array of length n which references the process control blocks (PCBs), indexed 0 to n1 - 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 a 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 What NOT to do: - Do NOT modify the choice values (1,2,3,4) or input characters and then try to convert them to integers--the test script used for grading your assignment will not work correctly. - Do NOT turn in an alternate version of the assignment downloaded from the Internet (coursehero, chegg, reddit, github, etc.) or submitted from you or another student from a previous semester - the test cases from this semester will not work on a previous semester's assignmentStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started