Question: With the given Java code using Netbeans IDE 8.2, how do I get the following output? DualCoreSimulator.java package dualcoresimulator; import java.util.Random; public class DualCoreSimulator {
With the given Java code using Netbeans IDE 8.2, how do I get the following output?
DualCoreSimulator.java
package dualcoresimulator;
import java.util.Random;
public class DualCoreSimulator { public static void main(String []args) throws HeapException{ double p = .2; int pid = 0; for(int x=1; x<=1000; x ++){ System.out.println("Cycle #" +x); { Random rand = new Random(); double number; number = rand.nextDouble(); if( number <= p*p){ System.out.println("Two new jobs this cycle"); for(int y=1; y <-2; y++){ pid=pid+1; System.out.println(pid); int length =1 +(int)(Math.random() * 100); System.out.println(length); int priority = -19 + (int) (Math.random()* 20); System.out.println(priority); } } else if (number > p*p & number <=p) { System.out.println("One new job this cycle."); pid=pid+1; System.out.println(pid); int length = 1 + (int)(Math.random() * 100); System.out.println(length); int priority = -19 + (int) (Math.random()* 20); System.out.println(priority); } else { System.out.println("No new job this cycle."); } } } } }
----------------------------------------------------------
HeapException.java
package dualcoresimulator;
public class HeapException extends Exception
{
/**
* Creates a new instance of HeapException without detail
* message.
*/
public HeapException()
{
}
/**
* Constructs an instance of HeapException with the specified
* detail message.
*
* @param msg the detail message.
*/
public HeapException(String msg)
{
super(msg);
}
}
-----------------------------------------------------------------------
HeapAPI.java
package dualcoresimulator;
public interface HeapAPI
/** * Inserts an item into the Heap. * @param item the value to be inserted. */ void insert(E item);
/** * An exception is generated if this method is invoked * by an empty heap. The maximum/minimum value is removed * from the heap if the heap is not empty and its effective * size is reduced by 1. * @return the maximum (in the case of a maxheap) or the * minimum (in the case of a minheap) on the heap. * @throws HeapException when the heap is empty */ E remove() throws HeapException;
/** * An exception is generated if this method is invoked * by an empty heap * @return the maximum (in the case of a maxheap) or the * minimum (in the case of a minheap) on the heap. * @throws HeapException when the heap is empty */ E peek() throws HeapException;
/** * Gives the size of this heap * @return the size of the heap; the effective size of the * heap. */ int size(); }
---------------------------------------------------------------
Heap.java
package dualcoresimulator;
import java.util.*;
public class Heap
{
/**
* A complete tree stored in an array list representing this
* binary heap
*/
private ArrayList
/**
* Constructs an empty heap
*/
public Heap()
{
tree = new ArrayList
}
public boolean isEmpty()
{
return tree.isEmpty();
}
public void insert(E obj)
{
tree.add(obj); // Put new value at end;
tree.add(element); // Put new value at end;
int loc = tree.size()-1; // and get its location
// Swap with parent until parent not larger
while (loc > 0 && tree.get(loc).compareTo(tree.get(parent(loc))) < 0) {
swap(tree, loc, parent(loc));
loc = parent(loc);
}
}
public E remove() throws HeapException
{
assert heapSize > 0 : "Cannot remove from an empty heap";
swap(0, --heapSize); // Swap root and sifting object
if (heapSize != 0) // Not an empty heap
siftDown(0); // perform sift process
return (E)heap[heapSize]; // Return original root
}
public E peek() throws HeapException
{
if (size == 0) throw new HeapException();
HeapAPI min = heap[1];
heap[1] = heap[size--];
percolatingDown(1);
return min;
}
public int size()
{
return tree.size();
}
/**
* Swaps a parent and child elements of this heap at the specified indices
* @param place an index of the child element on this heap
* @param parent an index of the parent element on this heap
*/
private void swap(int place, int parent)
{
int temp = items[place];
items[place] = items[parent];
items[parent] = temp;
}
/**
* Rebuilds the heap to ensure that the heap property of the tree is preserved.
* @param root the root index of the subtree to be rebuilt
*/
private void reheapify(int root)
{
int left = getLeft (root);
int right = getRight (root);
boolean winleft = compare (root, left) >= 0;
boolean winright = compare (root, right) >= 0;
if (winleft && winright) return;
if (compare (left, right) > 0) {
swap (root, left);
reheapify (left);
} else {
swap (root, right);
reheapify (right);
}
}
-----------------------------------------------
PCB.java
package dualcoresimulator;
public class PCB implements Comparable
/** * this process ID */ private int pid; /** * the nice (priority) value of this process */ private int priority; /** * running status 0=idle 1=running */ private int running; /** * cycle during which this process was created */ private int arrived; /** * length of time this process will take to execute */ private int length; /** * cycle during which this process begins executing */ private int start; /** * how long the process waits before it begins executing */ private int wait; /** * Creates a simulated job with default values for its parameters. */ public PCB() { priority = 19; running = 0; arrived = 0; length = 0; } /** * Creates a simulated job with the specified parameters. * @param iD the process id * @param pVal the priority value * @param run the running status * @param arr the arrival time * @param len the number of cycles this process takes to execute */ public PCB(int iD, int pVal, int run, int arr, int len) { pid = iD; priority = pVal; running = run; arrived = arr; length = len; }
/** * Gives the ID of this job. * @return the process ID */ public int getPid() { return pid; } /** * Gives the priority value of this process. * @return the priority value of this process */ public int getPriority() { return priority; }
/** * Indicates whether this process is executing.. * @return the execution status of this process */ public boolean isExecuting() { return running == 1; } /** * Sets the running status of this job. */ public void execute() { running = 1; } /** * Gives the cycle during which this process was creates * @return the cycle during which this process was created. */ public int getArrival() { return arrived; } /** * Gives the number of cycles required to execute this process. * @return the number of cycles required to execute this process */ public int getLength() { return length; } /** * Gives the cycle during which this process began executing. * @return the cycle during which this process began executing */ public int getStart() { return start; }
/** * Sets the cycle during which the process begins executing. * @param startCycle the cycle during which this process begins executing. */ public void setStart(int startCycle) { start = startCycle; } /** * Gives the number of cycles this process waited before executing. * @return the number of cycles from the process creation to its execution */ public int getWait() { return wait; }
/** * Sets the wait time for this process * @param waitTime the number of cycles that this process waited */ public void setWait(int waitTime) { wait = waitTime; }
/** * Compares this simulated job with the specified simulated job. * @param another a simulated process * @return 0 when this simulated job is the same as the specified * simulated job; -1 if this job will be executed after; otherwise 1. */ public int compareTo(PCB another) { if (running > another.running) return 1; if (running < another.running) return -1; if (priority < another.priority) return 1; if (priority > another.priority) return -1; if (arrived < another.arrived) return 1; if (arrived > another.arrived) return -1; return 0; } }
----------------------------------------------------------------------
What the output should look like.
run:
*** Cycle #: 1
CPU 1 (0) CPU 2 (0)
CPU 1: idle.
CPU 2: idle.
One new job this cycle.
CPU 1: Adding job with pid #1 and priority 19 and length 99.
*** Cycle #: 2
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 3
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 4
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 5
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 6
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 7
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 8
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 9
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 10
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 11
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 12
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 13
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 14
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
No new job this cycle.
*** Cycle #: 15
CPU 1 (1) CPU 2 (0)
CPU 1: Process #1 is executing.
CPU 2: idle.
One new job this cycle.
CPU 2: Adding job with pid #2 and priority 19 and length 59.
*** Cycle #: 16
CPU 1 (1) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 17
CPU 1 (1) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 18
CPU 1 (1) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 19
CPU 1 (1) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 20
CPU 1 (1) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 21
CPU 1 (1) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 22
CPU 1 (1) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 23
CPU 1 (1) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 24
CPU 1 (1) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
One new job this cycle.
CPU 1: Adding job with pid #3 and priority 5 and length 93.
*** Cycle #: 25
CPU 1 (2) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 26
CPU 1 (2) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 27
CPU 1 (2) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 28
CPU 1 (2) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 29
CPU 1 (2) CPU 2 (1)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
One new job this cycle.
CPU 2: Adding job with pid #4 and priority -19 and length 37.
*** Cycle #: 30
CPU 1 (2) CPU 2 (2)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
One new job this cycle.
CPU 1: Adding job with pid #5 and priority 0 and length 45.
*** Cycle #: 31
CPU 1 (3) CPU 2 (2)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 32
CPU 1 (3) CPU 2 (2)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 33
CPU 1 (3) CPU 2 (2)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
One new job this cycle.
CPU 2: Adding job with pid #6 and priority 7 and length 27.
*** Cycle #: 34
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 35
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 36
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 37
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 38
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 39
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 40
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 41
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 42
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 43
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 44
CPU 1 (3) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
One new job this cycle.
CPU 1: Adding job with pid #7 and priority 3 and length 33.
*** Cycle #: 45
CPU 1 (4) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 46
CPU 1 (4) CPU 2 (3)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
One new job this cycle.
CPU 2: Adding job with pid #8 and priority 16 and length 23.
*** Cycle #: 47
CPU 1 (4) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
One new job this cycle.
CPU 1: Adding job with pid #9 and priority -13 and length 43.
*** Cycle #: 48
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 49
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 50
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 51
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 52
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 53
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 54
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 55
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 56
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 57
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 58
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 59
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 60
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 61
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 62
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 63
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 64
CPU 1 (5) CPU 2 (4)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
One new job this cycle.
CPU 2: Adding job with pid #10 and priority -2 and length 53.
*** Cycle #: 65
CPU 1 (5) CPU 2 (5)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
*** Cycle #: 66
CPU 1 (5) CPU 2 (5)
CPU 1: Process #1 is executing.
CPU 2: Process #2 is executing.
No new job this cycle.
.
.
.
.
.
.
.
*** Cycle #: 986
CPU 1 (123) CPU 2 (123)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
*** Cycle #: 987
CPU 1 (123) CPU 2 (123)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
*** Cycle #: 988
CPU 1 (123) CPU 2 (123)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
One new job this cycle.
CPU 1: Adding job with pid #284 and priority -18 and length 62.
*** Cycle #: 989
CPU 1 (124) CPU 2 (123)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
*** Cycle #: 990
CPU 1 (124) CPU 2 (123)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
*** Cycle #: 991
CPU 1 (124) CPU 2 (123)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
*** Cycle #: 992
CPU 1 (124) CPU 2 (123)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
*** Cycle #: 993
CPU 1 (124) CPU 2 (123)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
*** Cycle #: 994
CPU 1 (124) CPU 2 (123)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
One new job this cycle.
CPU 2: Adding job with pid #285 and priority -10 and length 65.
*** Cycle #: 995
CPU 1 (124) CPU 2 (124)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
Two new jobs this cycle.
CPU 1: Adding job with pid #286 and priority -11 and length 80.
CPU 2: Adding job with pid #287 and priority 7 and length 66.
*** Cycle #: 996
CPU 1 (125) CPU 2 (125)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
*** Cycle #: 997
CPU 1 (125) CPU 2 (125)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
*** Cycle #: 998
CPU 1 (125) CPU 2 (125)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
Two new jobs this cycle.
CPU 1: Adding job with pid #288 and priority 18 and length 68.
CPU 2: Adding job with pid #289 and priority 1 and length 82.
*** Cycle #: 999
CPU 1 (126) CPU 2 (126)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
*** Cycle #: 1000
CPU 1 (126) CPU 2 (126)
CPU 1: Process #267 is executing.
CPU 2: Process #278 is executing.
No new job this cycle.
CPU 1: average throughput is 0.017 per cycle.
CPU 1: average wait time is 103.82352941176471.
CPU 2: average throughput is 0.02 per cycle.
CPU 2: average wait time is 105.88235294117646.
overall average throughput is 0.037 per cycle.
overall average wait time is 96.35135135135135.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
