Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

One of the main applications of priority queues is in operating systems for sche

ID: 3763404 • Letter: O

Question

One of the main applications of priority queues is in operating systems for scheduling jobs on a CPU. In this project you are to build a program that schedules simulated CPU jobs. Your program should run in a loop, each iteration of which corresponds to a time slice for the CPU. Each job is assigned a priority, which is an integer between -20 (highest priority) and 19 (lowest priority), inclusive. From among all jobs waiting to be processed in a time slice, the CPU must work on a job with highest priority. In this simulation, each job will also come with a length value, which is an integer between 1 and 100, inclusive, indicating the number of time slices that are needed to process this job. For simplicity, you may assume jobs cannot be interrupted—once it is scheduled on the CPU, a job runs for a number of time slices equal to its length. Your simulator must output the name of the job running on the CPU in each time slice and must process a sequence of commands, one per time slice, each of which is of the form "add job name with length n and priority p" or "no new job this slice".

Explanation / Answer

CLASS JOB

public class Job implements Comparable<Job>{
  
   public Job(int priority, int length, String name) throws Exception {
       if (priority >= -20 && priority <= 19)
           this.priority = priority;
       else
           throw new Exception("Priority is out of range");

       if (length <= 100 && length >= 1)
           this.length = length;
       else
           throw new Exception("Length is out of range");
      
       this.name = name;
   }
   private int priority;
   private int length; // number of time slices
   private String name;
   public int getPriority() {
       return priority;
   }
   public void setPriority(int priority) throws Exception {
       if (priority >= -20 && priority <= 19)
           this.priority = priority;
       else
           throw new Exception("Priority is out of range");
   }
   public int getLength() {
       return length;
   }
   public void setLength(int length) throws Exception {
       if (length <= 100 && length >= 1)
           this.length = length;
       else
           throw new Exception("Length is out of range");
   }
   @Override
   public int compareTo(Job job) {
       return (this.priority + job.priority);
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
}

}

CLASS CPU

import java.util.PriorityQueue;

public class CPU {
  
   private PriorityQueue<Job> queue = new PriorityQueue<>();
   private Job currentJob = null;
   public void addJob(Job job) {
       this.queue.add(job);
   }
  
   public void runCPU() {
       while (!queue.isEmpty()) {
           currentJob = queue.remove();
           System.out.println("add " + currentJob.getName() + " with length " + currentJob.getLength() + " and priority " + currentJob.getPriority());
           for (int i = 1; i < currentJob.getLength() ; i++)
               System.out.println("no new job this slice");          
       }
   }
  
   public static void main(String[] args) throws Exception {
       CPU cpu = new CPU();
       Job job1 = new Job(-4, 2, "Job1");
       Job job2 = new Job(-10, 3, "Job2");
       cpu.addJob(job1);
       cpu.addJob(job2);
       cpu.runCPU();
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote