Question: Application #4 Using ArrayDeque and PriorityQueue classes from java.util write a program to simulate job scheduling in an operating system. Jobs are generated at random

Application #4
Using ArrayDeque and PriorityQueue classes from java.util write a program to simulate job scheduling in an operating system.
Jobs are generated at random times.
Each job is given:
a random priority from 1 to 4, where 1 is the highest priority
a random amount of time to complete its execution.
Jobs do not begin execution and run to completion, but instead share the processor. The operating system executes a job for a fixed unit of time called a time slice. At the end of the time slice, the current jobs execution is suspended, and the job is placed back on the priority queue, where it waits for its next share of processor time. The job having the highest priority is then removed from the priority queue and executed for the time slice.
When a job is first generated, it will begin executing immediately if the processor is free. Otherwise it will be placed on the priority queue.
Since the job scheduling can be done not only by the priority but also by some other attributes add ability to run the simulation in two modes:
1. where jobs are ordered by their priority (compareTo)
2. where jobs are ordered by their timeLeft (need to implement Comparator)
SchedulingSimulation constructor would take a parameter which will indicate the priority queue ordering: SORT_BY_PRIORITY or SORT_BY_LENGTH
UML Diagram(*)
(At the very bottom)
(*) please note that :
1. Job class has implementation of compareTo method, so it must be declared as:
public class Job implements Comparable
2. With compareTo method implemented, we can create PriorityQueue that organizes entries by the natural ordering as follow:
this.waitingJobs = new PriorityQueue();
3. To create a PriorityQueue that organizes elements NOT by the natural ordering we need to implement compare method that is defined by the Comparator interface. We can create appropriate PriorityQueue utilizing annonymous inner class as follow:
this.waitingJobs = new PriorityQueue(new Comparator()
{
public int compare(Job job1, Job job2)
{
return job1.getTimeLeft() - job2.getTimeLeft();
}
});
In the attached sample run the simulation constants were set as follow:
TIME_SLICE_PER_JOB = 3;
SIMULATION_DURATION = 100;
JOB_PROBABILITY = 30;
JOB_PRIORITY = 4;
JOB_MIN_TIME = 1;
JOB_MAX_TIME = 5;
Sample Run #1:
***STARTING THE SIMULATION WITH PRIORITY MODE SET TO SORT_BY_PRIORITY***
Time Marker 0 waiting: 0
executing: NONE
created: Job #1 priority(1) created at 0, time left 4
Time Marker 1 waiting: 0
executing: Job #1 priority(1) created at 0, time left 4
Time Marker 2 waiting: 0
executing: Job #1 priority(1) created at 0, time left 3
created: Job #2 priority(2) created at 2, time left 5
Time Marker 3 waiting: 1
executing: Job #1 priority(1) created at 0, time left 2
Time Marker 4 waiting: 1
executing: Job #1 priority(1) created at 0, time left 1
completed: Job #1 at time 4
 Application #4 Using ArrayDeque and PriorityQueue classes from java.util write a

JobNo nt int int nt int priority createdAtTime a timeLeft jobsCreated mJob(int, int, int) compareTo(Job) nt nt getJobsCreated int update(int) isFinished) boolean toStringo void String creat SchedulingSimulation allJobs TIME SLICE PER JOE waitingJobs Priority QueuecJob> ArrayDeque Job int int int int int int int int a current Job SIMULATION DURATION JOB PROBABILITY JOB PRIORITY JOB MIN TIME JOB MAX TIME SORT BY PRIORITY '. SORT-BY-LENGTH mSchedulingSimulatlon(int) m runSimulation) mgenerat eJob(int) void void mreportAtTimeMarker(int) void void void finalS mulationReport) main(Stringl)

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!