QuestionQuestion

Homework Assignment 4

This assignment has two primary goals. The first goal is to give students an opportunity to study the implementation of the adaptable heap priority queue, which is discussed in Chapter 9. We discuss the outline of the implementation in the class. However, we do not discuss implementation-level details. Students are expected to acquire a deeper understanding of the implementation through this assignment
The second goal is to give students an opportunity to implement an application that uses a priority queue. The application to be implemented is a simulation of a process scheduler of a computer system. Though the simulated scheduler is a small, simplified version, it reflects some of the basic operations of a typical process scheduler.
Implement this program as ProcessScheduling.java. You may have multiple classes and multiple files but your main method must be in the ProcessScheduling.java file.

The following describes the scheduling system that is simulated.
Processes arrive at a computer system and the computer system executes the processes one at a time based on a priority criterion. Each process has a process id, priority, arrival time, and duration. The duration of a process is the amount of time that takes to completely execute the process. The system keeps a priority queue to keep arriving processes and prioritize the execution of processes. When a process arrives, it is inserted into the priority queue. Then, each time the system is ready to execute a process, the system removes a process with the smallest priority from the priority queue and executes it for the duration of the process.

Suppose that a process p with a very large priority arrives at the system while the system is executing another process. While p is waiting in the priority queue, another process q with a smaller priority arrives and it is added to the priority queue. After the execution of the current process is finished, the system will remove q from the priority queue and execute it instead of p (because q has a smaller priority), and p must wait until q finishes. If another process with a smaller priority arrives while q is being executed, p will have to wait again after q is completed. If this is repeated, p will have to wait for a very long time. One way of preventing this is as follows: If a process has waited longer than a predetermined maximum wait time, its priority is decremented.

For the purpose of this simulation, we assume the followings:

We use the priority queue implemented in the HeapAdaptablePriorityQueue.java. This class implements an adaptable priority queue that uses a heap data structure.

Each entry in the priority queue keeps (key, value) pair, which represents a process.
The key of an entry is the priority of the corresponding process and it is of Integer type.
The value of priority is between 1 and 10, inclusively. A process with a smaller priority is executed before a process with a larger priority.
The value of an entry is the reference to the corresponding process.
Each process must have, at the minimum, the following attributes:
pr: Integer // priority of the process
id: integer // process id
arrivalTime: integer // the time when the process arrives at the system
duration: integer // execution of the process takes this amount of time

The simulation program uses a logical time to keep track of the simulation process and the same logical time is used to represent the arrivalTime and duration. The simulation goes through a series of iterations and each iteration represents the passage of one logical time unit (in what follows we will use time unit to refer to logical time unit). At the beginning, the current time is set to time 0. Each iteration implements what occurs during one time unit and, at the end of each iteration, the current time is incremented.

The following describes the simulation program:
All processes are stored in a certain data structure D, which is supposed to be external to the computer system.
In each iteration, the following occurs:
We compare the current time with the arrival time of a process with the earliest arrival time in D. If the arrival time of that process is equal to or smaller than the current time, we remove the process from D and insert it into the priority queue Q (this represents the arrival of a process at the system).

If no process is being executed at this time and there is at least one process in Q, then a process with the smallest priority is removed from Q and executed.

Note: When there is more than one process with the same smallest priority, it would be reasonable that the one with the earliest arrivalTime is removed from Q and executed. However, for this assignment, you don’t need to implement this and you just need to remove an entry with the smallest priority as determined by the code in

HeapAdaptablePriorityQueue.java.

If the currently running process is completed, then we update the priorities of some processes. The update is performed as follows. If there is any process in Q that has been waiting longer than the maximum wait time, then the priority of that process is decreased by one.

Note: We assume that the priorities of “long-waiting” processes are updated only when the execution of a process is finished.

The current time is increased by one time unit.

The above is repeated until D is empty. At this time, all processes have arrived at the system. Some of them may have been completed and removed from Q and some may still wait in Q.

If there are any remaining processes in Q, these processes are removed and executed one at a time. Again, a process with the smallest priority is removed and executed first.

A pseudocode of the simulation is given below. This pseudocode is a high-level code and details are not included. The omission of the details is intentional and students are expected to figure out the details. In the pseudocode, Q is a priority queue, which stores (priority, process) pairs. There is a Boolean variable running in the pseudocode. It is used to indicate whether the system is currently executing a process or not. It is true if the system is currently executing a process and false otherwise.

Read all processes from the input file and store them in an appropriate data structure, D

currentTime = 0
running = false
create an empty priority queue Q
While D is not empty // while loop runs once for every time unit until D is empty
Get (don’t remove) a process p from D that has the earliest arrival time
If the arrival time of p <= currentTime
Remove p from D and insert it into Q
If Q is not empty and the flag running is false
Remove a process with the smallest priority from Q
Set a flag running to true currentTime = currentTime + 1
If currently running process has finished
Set a flag running to false
Update priorities of processes that have been waiting longer than max. wait time
// At this time all processes in D have been moved to Q.
If there are any remaining processes in Q
Remove them one at a time and execute it.
As mentioned in the first line of the pseudocode, an input file stores information about all
processes. A sample input file is shown below:
1 7 8 9
2 1 6 13
3 9 19 20
4 2 10 25
5 4 19 34
6 10 11 43
7 4 12 45
8 1 18 54
9 3 11 63
10 3 7 73

Each line in the input file represents a process and it has four integers separated by a space(s). The four integers are the process id, priority, duration, and arrival time, respectively, of a process. Your program must read all processes from the input file and store them in an appropriate data structure. You can use any data structure that you think is appropriate. However, for the priority queue, you must use the priority queue implemented in

HeapAdaptablePriorityQueue.java. The HeapAdaptablePriorityQueue class inherits from or implements a few superclasses and interfaces. You may want to study some or all of these superclasses and interfaces to better understand the HeapAdaptablePriorityQueue class. On Blackboard, I will post only the HeapAdaptablePriorityQueue.java file. You can obtain other files from the textbook’s web site.

While your program is performing the simulation, whenever a process is removed from the priority queue (to be executed), your program must display information about the removed process. As discussed earlier, the priority of a process is decremented if it has waited longer than the predefined maximum waiting time. If an update happens, your program must issue an output (refer to the sample output shown below). After your program finishes the simulation of the execution of all processes in the input file, it must display the average waiting time of all processes. A sample output is shown below, which is the expected output for the above sample input. In this example, the maximum wait time is 10.

Id = 1, priority = 7, duration = 8, arrival time = 9

Id = 2, priority = 1, duration = 6, arrival time = 13

Id = 3, priority = 9, duration = 19, arrival time = 20

Id = 4, priority = 2, duration = 10, arrival time = 25

Id = 5, priority = 4, duration = 19, arrival time = 34

Id = 6, priority = 10, duration = 11, arrival time = 43

Id = 7, priority = 4, duration = 12, arrival time = 45

Id = 8, priority = 1, duration = 18, arrival time = 54

Id = 9, priority = 3, duration = 11, arrival time = 63

Id = 10, priority = 3, duration = 7, arrival time = 73

Process removed from queue is: id = 1, at time 9, wait time = 0

Process id = 1

Priority = 7

Arrival = 9

Duration = 8

Process 1 finished at time 17

Process removed from queue is: id = 2, at time 17, wait time = 4

Process id = 2

Priority = 1

Arrival = 13

Duration = 6

Process 2 finished at time 23

Process removed from queue is: id = 3, at time 23, wait time = 3

Process id = 3

Priority = 9

Arrival = 20

Duration = 19

Process 3 finished at time 42

Priority of process 4 decremented

Process removed from queue is: id = 4, at time 42, wait time = 17

Process id = 4

Priority = 1

Arrival = 25

Duration = 10

Process 4 finished at time 52

Prioirty of process 5 decremented

Process removed from queue is: id = 5, at time 52, wait time = 18

Process id = 5

Priority = 3

Arrival = 34

Duration = 19

Process 5 finished at time 71

Prioirty of process 7 decremented

Prioirty of process 6 decremented

Process removed from queue is: id = 8, at time 71, wait time = 17

Process id = 8

Priority = 1

Arrival = 54

Duration = 18

Process 8 finished at time 89

Prioirty of process 9 decremented

Prioirty of process 10 decremented

Prioirty of process 7 decremented

Prioirty of process 6 decremented

D is empty

Process removed from queue is: id = 9, at time 89, wait time = 26

Process id = 9

Priority = 2

Arrival = 63

Duration = 11

Process 9 finished at time 100

Prioirty of process 10 decremented

Prioirty of process 6 decremented

Prioirty of process 7 decremented

Process removed from queue is: id = 10, at time 100, wait time = 27

Process id = 10

Priority = 1

Arrival = 73

Duration = 7

Process 10 finished at time 107

Prioirty of process 6 decremented

Process removed from queue is: id = 7, at time 107, wait time = 62

Process id = 7

Priority = 1

Arrival = 45

Duration = 12

Process 7 finished at time 119

Prioirty of process 6 decremented

Process removed from queue is: id = 6, at time 119, wait time = 76

Process id = 6

Priority = 5

Arrival = 43

Duration = 11

Process 6 finished at time 130

Total wait time = 250.0

Average wait time = 25.0

Note that the priority of a process shown in the output is not necessarily the initial priority of the process. As mentioned earlier, the priority of a process may be updated if it has waited beyond the maximum wait time. So, the priorities in the output are the final ones that might have been updated. Your program must write the output to an output file named process_scheduling_out.txt.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.io.*;
import java.util.*;

import net.datastructures.Entry;
import net.datastructures.HeapPriorityQueue;
import net.datastructures.HeapAdaptablePriorityQueue;

/*
* Process Scheduling class
*/
public class ProcessScheduling {
   
    static int MAXIMUM_WAIT_TIME = 10;
    static String outputFile = "process_scheduling_out.txt";
    static FileWriter fw;
   
    // Method to run simulation
    public static void simulation(HeapPriorityQueue<Integer,Process> D) throws IOException {
       int currentTime = 0;
       Process runningProcess = D.min().getValue();
       boolean running = false;
       HeapAdaptablePriorityQueue<Integer,Process> Q = new HeapAdaptablePriorityQueue<Integer,Process>();
       double totalWaitTime = 0.0;
       int count = D.size();
      
       while (!D.isEmpty()) {
            Process p = D.min().getValue();
            if (p.getArrivalTime() <= currentTime) {
                D.removeMin();
                Q.insert(p.getPriority(), p);
            }
            if (!Q.isEmpty() && running == false) {
                runningProcess = Q.removeMin().getValue();
                runningProcess.setStartTime(currentTime);
                totalWaitTime += runningProcess.getWaitTime();
                fw.write("Process removed from queue is: id = " + runningProcess.getID() + ", at time " + currentTime + ", wait time = " + runningProcess.getWaitTime() + '\n');
                fw.write("Process id = " + runningProcess.getID() + '\n');
                fw.write("       " + "Priority = " + runningProcess.getPriority() + '\n');
                fw.write("       " + "Arrival = " + runningProcess.getArrivalTime() + '\n');
                fw.write("       " + "Duration = " + runningProcess.getDuration() + '\n');
                running = true;
            }
            currentTime++;
            if (D.isEmpty()) {
                while (runningProcess.getFinishTime() > currentTime) {
                   currentTime++;
                }
            }
            if (runningProcess.getFinishTime() <= currentTime) {
                fw.write("Process " + runningProcess.getID() + " finished at time " + currentTime + '\n');
                running = false;
                decrementPriorities(currentTime, Q);
                fw.write('\n');
            }
       }
       fw.write("D is empty" + '\n' + '\n');
       while (!Q.isEmpty()) {
            if (!Q.isEmpty() && running == false) {
                runningProcess = Q.removeMin().getValue();
                runningProcess.setStartTime(currentTime);
                totalWaitTime += runningProcess.getWaitTime();
                fw.write("Process removed from queue is: id = " + runningProcess.getID() + ", at time " + currentTime + ", wait time = " + runningProcess.getWaitTime() + '\n');
                fw.write("Process id = " + runningProcess.getID() + '\n');
                fw.write("       " + "Priority = " + runningProcess.getPriority() + '\n');
                fw.write("       " + "Arrival = " + runningProcess.getArrivalTime() + '\n');
                fw.write("       " + "Duration = " + runningProcess.getDuration() + '\n');
                running = true;
            }
            currentTime++;
            if (Q.isEmpty()) {
                while (runningProcess.getFinishTime() > currentTime) {
                   currentTime...
$60.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats