// Code skeleton for a solution to P-8.10 of our text. public static class JobScheduler { public static class Job { String name; int arrival_time; int service_time; int priority; // smaller priorities are higher priority } private Queue arrival_queue; private PriorityQueue service_queue; int time_now = 0; private boolean working = false; // true if a job is currently in progress private Job active_job; // the job in progress // Load the sequence of arrivals into the arrival_queue from a file. private void load_queue (String file_name) { .... } // Run the scheduler, taking input from the arrival queue private void run_scheduler { // Run simulation while there is still work to do: // either a job in the arrival queue // or a job in the service queue // or a job that is active. while (working || !arrival_queue.isEmpty() || !service_queue.isEmpty() { // Put jobs into the service queue that arrive at the current time while (! arrival_queue.isEmpty() ) { Job j = arrival_queue.front(); if ((j.arrival_time == time) { service_queue.insert(j.priority, j); arrival_queue.dequeue(); } else break; } // Activate a job if none are running and the service_queue is non-empty. if (!working and !service_queue.isEmpty()) { active_job = (service_queue.min()).getValue(); working = true; } // Now if there is an active job: run it for a time quantum. if (working == true) { System.out.println (time, ": ", active_job.name); // decrement service time. if zero, unset working bit if (-- active_job.service_time == 0) working = false; } else { // working == false System.out.println (time, " -- no job --"); } time_now ++; } } }