Question: Write code in java to simulate the following CPU scheduling algorithm Shortest Job First (SJF). Assume no-preemption but take arrival times into account. To do

Write code in java to simulate the following CPU scheduling algorithm

Shortest Job First (SJF).

Assume no-preemption but take arrival times into account. To do that, you have to simulate the time (by simply defining an integer variable that keeps track of the time). At any given point in time, your scheduler should consider only the processes that have arrived. Whenever a new process arrives, add it to a priority queue in which the key is the CPU burst length. After Time 0, scheduling decisions are made only when the process that currently has the CPU terminates. The scheduling decision will be giving the CPU to the process with the shortest CPU burst. Ties must be broken based on the arrival time, that is, if the ready queue has two or more processes with the same CPU burst, these processes get the CPU in the order they arrived (FCFS). If multiple processes have the same CPU burst and the same arrival time, ties are broken arbitrarily.

Your program should read an input file named input.txt and write the results into an output file named output.txt. The formats of these files are as follows:

Input File:

The first line has a space-separated list of scheduling algorithm names to run (any combination of the four names given above).

The second line has a single integer representing the number of processes in the file.

The rest of the file has one line per process with the following information:

Process number Arrival Time CPU burst time Priority

If multiple processes have the same arrival time, your scheduling algorithm should assume that there are negligibly small differences in arrival times with the processes arriving in the order they appear in the file. For priority scheduling, assume that smaller numbers indicate higher priorities. Non-priority algorithms will simply ignore the priority field.

Output File:

The output file will have the scheduling results for each of the algorithms listed in the input file. There should be one line for each CPU assignment (one line for each vertical line in the Gantt chart). Each line should have two numbers: one indicating the time point and one indicating the process number that got the CPU at that point. The last line for each scheduling algorithm should have the average waiting time.

Examples:

Input 2 (from the book)

SJF

4

1 0 6 1

2 0 8 1

3 0 7 1

4 0 3 1

Output 2

SJF

0 4

3 1

9 3

16 2

AVG Waiting Time: 7

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!