Question
In JAVA please and please comment on your code. Thank you CSCI 331 Project One Note: This is a project from your zybook textbook. Project
In JAVA please and please comment on your code. Thank you
CSCI 331 Project One
Note: This is a project from your zybook textbook.
Project objective
Compare the performance of process creation and destruction when implemented with and without linked lists.
Description
Version 1 of the process creation hierarchy uses linked lists to keep track of child processes as described in section "The process control block", subsection "The PCB data structure".
For the purposes of performance evaluation, the PCBs are simplified as follows:
- All PCBs are implemented as an array of size n.
- Each process is referred to by the PCB index, 0 through n-1.
- Each PCB is a structure consisting of only the two fields:
- parent: a PCB index corresponding to the process's creator
- children: a pointer to a linked list, where each list element contains the PCB index of one child process
The necessary functions are simplified as follows:
- create(p) represents the create function executed by 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
- create a new link containing the child's index q and appends the link to the linked list of PCB[p]
- destroy(p) represents the destroy function executed by 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 progenies */
- free PCB[q]
- deallocate the element q from the linked list
- for each element q on the linked list of children of PCB[p]
Version 2 of the same process creation hierarchy uses no linked lists. Instead, each PCB contains the 4 integer fields parent, first_child, younger_sibling, and older_sibling, as described in the subsection "Avoiding linked lists".
Assignment
- Implement the two versions of the process creation hierarchy.
- Assume that PCB[0] is the only currently existing process and write a test program that performs a series of process creations and destructions. Ex:
cr[0] /* creates 1st child of PCB[0] at PCB[1]*/
cr[0] /* creates 2nd child of PCB[0] at PCB[2]*/
cr[2] /* creates 1st child of PCB[2] at PCB[3] */
cr[0] /* creates 3rd child of PCB[0] at PCB[4] */
de[0] /* destroys all descendents of PCB[0], which includes processes PCB[1] through PCB[4] */
- Run the test program repeatedly in a long loop using both versions and compare the running times to see how much time is saved in version 2, which avoids dynamic memory management.
-------------------------------------------------------------------------------------------------
PCBS.java(interface)
public interface PCBS
{
/** create a child process of process p
*If process p not exist, return error message "No such a parent process"
*If this process control blocks is full, return "No space for new process"
*Return null if the creation is success
*@param p the process index of parent process
*/
public String create(int p):
/**
*destroy the process control block at index p
*/
public void destroy(int p);
}
Test.java
public class Test
{
public static long test(int n, int version){
PCBS pcbs;
if(version == 1)
pcbs = new ProcessControlBlocks(100);
else
pcbs = new ProcessControlBlocks2(100);
long start = System.currentTimeMillis();
for(int i = 0; I < n; i++){
pcbs.create(0); // child process at location 1
pcbs.create(0); // child process at location 2
pcbs.create(2); // child process at location 3
pcbs.create(3); // child process at location 4
pcbs.create(0); // child process at location 5
pcbs.destroyer(2); // destroy all child process
pcbs.destroyer(1);
pcbs.destroyer(5);
}
long end = System.currentTimeMillis();
return end - start;
}
public static void main(String [] args){
// easy to modify it to allow user to input how many time to do the loop
// here I used 1000000 times
long t1 = test(1000000, 1); // version 1 time
long t2 = test(1000000, 2); // version 2 time
System.out.println("Version 1 used " + t1 + " milliseconds");
System.out.println("Version 2 used " + t2 + " milliseconds");
if(difference > 0)
System.out.println("Version 1 is " + difference + " milliseconds slower");
else
System.out.println("Version 2 is " + difference + " milliseconds slower");
}
}
--------------------------------------
output:
version 1 used 508 milliseconds
version 2 used 165 milliseconds
version 1 is 343 milliseconds slower
Step 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