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

1) Write a program that will simulate FCFS, SJN, SRT, and Round Robin scheduling

ID: 3731387 • Letter: 1

Question

1) Write a program that will simulate FCFS, SJN, SRT, and Round Robin scheduling algorithms. For each algorithm, the program should compute waiting time and turnaround time of every job as well as the average waiting time and average turnaround time. The average values should be consolidated in a table for easy comparison. You may use the following data to test your program. The time quantum for round robin is 4 ms. (Assume that context switching time is 0). Job Arrival Times CPU Cycle (in milliseconds) 0 10 12 14 16 17 19 14

Explanation / Answer

// FCFS

import java.util.*;

// Class FCFS_CPU definition for First Come First Serve CPU scheduling

public class FCFS_CPU

{

// main function definition

public static void main(String ss[])

{

// Scanner class object created

Scanner sc = new Scanner(System.in);

// Instance variable to store number of processes

int numberOfProcess;

// Instance variable to store average waiting time and average turn around time

float avgWT = 0, avgTAT = 0;

// Accepts number of processes from the user

System.out.print("Enter number of processes: ");

numberOfProcess = sc.nextInt();

// Creates an array to store burst time of each process

int burstTime[] = new int[numberOfProcess];

// Creates an array to store process name of each process

char process[] = new char[numberOfProcess];

// Creates an array to store waiting time of each process

int waitingTime[] = new int[numberOfProcess];

// Creates an array to store turn around time of each process

int turnAroundTime[] = new int[numberOfProcess];

// Loops till number of processes

for(int c = 0; c < numberOfProcess; c++)

{

// Stores the process name by adding 65 ASCII value of 'A' with counter value and converts it to character

process[c] = (char)(c + 65);

// Displays the message for process name

System.out.printf("Enter burst time for process %c: ", process[c]);

// Accepts burst time and stores it in array

burstTime[c] = sc.nextInt();

}// End of for loop

// Initializes waiting time to zero for the first process  

waitingTime[0] = 0;

// Loops till number of processes

for(int c = 1; c < numberOfProcess; c++)

{

// Calculates waiting time of the next process

// by adding burst time of each process with waiting time of each process

waitingTime[c] = burstTime[c - 1] + waitingTime[c - 1];

// Calculates total waiting time

avgWT += waitingTime[c];

}// End of for loop

// Loops till number of processes

for(int c = 0; c < numberOfProcess; c++)

{

// Calculate turn around time of each process

// by adding burst time of each process with waiting time of each process

turnAroundTime[c] = burstTime[c] + waitingTime[c];

// Calculates total turn around time

avgTAT += turnAroundTime[c];

}// End of for loop

// Displays the heading

System.out.println("Process Burst time Waiting time Turn Around time");

// Loops till number of processes

for(int c = 0; c < numberOfProcess; c++)

{

// Displays each process information

System.out.println(process[c] + " " + burstTime[c] + " " + waitingTime[c] + " " + turnAroundTime[c]);

}// End of for loop

// Calculates and displays average waiting time and turn around time

System.out.println("Average Waiting time = " + avgWT / numberOfProcess);

System.out.println("Average Turn Around time = " + avgTAT / numberOfProcess);

// Closes the scanner class

sc.close();

}// End of main method

}// End of class

Sample Output:

Enter number of processes: 10

Enter burst time for process A: 16

Enter burst time for process B: 2

Enter burst time for process C: 11

Enter burst time for process D: 6

Enter burst time for process E: 1

Enter burst time for process F: 9

Enter burst time for process G: 4

Enter burst time for process H: 14

Enter burst time for process I: 1

Enter burst time for process J: 8

Process Burst time Waiting time Turn Around time

A 16 0 16

B 2 16 18

C 11 18 29

D 6 29 35

E 1 35 36

F 9 36 45

G 4 45 49

H 14 49 63

I 1 63 64

J 8 64 72

Average Waiting time = 35.5

Average Turn Around time = 42.7

----------------------------------------------------------------------------------------

// SJN

import java.util.Scanner;

// Defines a class for Process

class Process

{

// To store process ID

char processID;

// To store burst time

int burstTime;

// To store arrival time

int arrivalTime;

// Constructor to initialize a process

public Process(char pid, int bt, int art)

{

processID = pid;

burstTime = bt;

arrivalTime = art;

}// End of constructor

}// End of class

// Class SJF_CPU_Arrival definition for Shortest Job First CPU scheduling

public class SJF_CPU_Arrival

{

// static method to find the waiting time for all processes

static void waitingTime(Process procss[], int numberOfProcess, int waitingTime[])

{

// To store remaining time of each process

int remainingTime[] = new int[numberOfProcess];

// Loops till number of processes and copy the burst time into remainingTime[] for each process

for (int c = 0; c < numberOfProcess; c++)

remainingTime[c] = procss[c].burstTime;

// To store complete status of each process

int complete = 0;

// To store current time information

int currentTime = 0;

// To keep track of shortest time, initializes to max value

int minmum = Integer.MAX_VALUE;   

int shortest = 0, finishTime;

// Initializes status to false for all process

boolean check = false;

  

// Process until all processes gets completed

while (complete != numberOfProcess)

{   

// Loops till number of processes

// and find process with minimum remaining time among the processes that arrives till the current time`

for (int d = 0; d < numberOfProcess; d++)

{

// Checks if current process arrival time is less than or equals to current time

// and remaining time of current process is less than the earlier minimum

// and remaining time of current process is greater than zero i.e., no finished

if ((procss[d].arrivalTime <= currentTime) && (remainingTime[d] < minmum) && remainingTime[d] > 0)

{

// Set the minimum to current process remaining time

minmum = remainingTime[d];

// Set the shortest to current process counter number

shortest = d;

// Set the check status to true

check = true;

}// End of if condition

}// End of for loop

// Checks if the check status is false

if (check == false)

{

// Increase the current time by one

currentTime++;

// Continue the loop

continue;

}// End of if condition

  

// Reduce remaining time by one for shortest process

remainingTime[shortest]--;

  

// Update minimum to the remaining time of shortest process

minmum = remainingTime[shortest];

// Checks if the minimum is zero

if (minmum == 0)

// Reset the value to maximum value

minmum = Integer.MAX_VALUE;

  

// Checks if a process gets completed by checking remaining time of the shortest process is zero

if (remainingTime[shortest] == 0)

{   

// Increment complete counter by one

complete++;

  

// Calculates the finish time of current process by adding current time with one

finishTime = currentTime + 1;

  

// Calculate waiting time of each process

// by subtracting shortest process burst time from shortest process arrival time from finish time

waitingTime[shortest] = finishTime - procss[shortest].burstTime - procss[shortest].arrivalTime;

// Checks if waiting time of the shortest process is less than zero

if (waitingTime[shortest] < 0)

// Set the waiting time of the shortest process to zero

waitingTime[shortest] = 0;

}// End of if condition

// Increment the current time by one

currentTime++;

}// End of while loop

}// End of method

  

// Static method to calculate turn around time

static void turnAroundTime(Process process[], int numberOfProcess, int waitingTime[], int turnAroundTime[])

{

// Loops till number of process

for (int c = 0; c < numberOfProcess; c++)

// Calculates turn around time by adding burst time of each process with waiting time of each process

turnAroundTime[c] = process[c].burstTime + waitingTime[c];

}// End of method

  

// Static method to calculate average time

static void findavgTime(Process process[], int numberOfProcess)

{

// Creates an array to store waiting time of each process

int waitingTime[] = new int[numberOfProcess];

// Creates an array to store turn around time of each process

int turnAroundTime[] = new int[numberOfProcess];

// To store total waiting time and turn around time and initializes it to zero

int totalWaitingTime = 0, totalTAT = 0;

  

// Calls the function to find waiting time of all processes

waitingTime(process, numberOfProcess, waitingTime);

  

// Calls the function to find turn around time for all processes

turnAroundTime(process, numberOfProcess, waitingTime, turnAroundTime);

  

// Display heading

System.out.println("Processes " + " Burst time " + " Waiting time " + " Turn around time");

  

// Loops till number of processes

for (int c = 0; c < numberOfProcess; c++)

{

// Calculate total waiting time and total turn around time

totalWaitingTime = totalWaitingTime + waitingTime[c];

totalTAT = totalTAT + turnAroundTime[c];

// Displays each process information

System.out.println(process[c].processID + " " + process[c].burstTime

+ " " + waitingTime[c] + " " + turnAroundTime[c]);

}// End of for loop

// Calculates and displays average waiting time and turn around time

System.out.println("Average waiting time = " + (float)totalWaitingTime / (float)numberOfProcess);

System.out.println("Average turn around time = " + (float)totalTAT / (float)numberOfProcess);

}// End of method

// main method definition

public static void main(String[] args)

{

// Scanner class object created

Scanner sc = new Scanner(System.in);

// Accepts number of process

System.out.print(" Enter number of process: ");

int numberOfProcess = sc.nextInt();

// Creates an array to store burst time of each process

int burstTime[] = new int[numberOfProcess];

// Creates an array to store arrival time of each process

int arrivalTime[] = new int[numberOfProcess];

// Crates an array of objects

Process process[] = new Process[numberOfProcess];

// Loops till number of processes to accept burst time

for(int c = 0; c < numberOfProcess; c++)

{

// Displays process name

System.out.printf(" Enter burst time for process %c: ", (char)(c + 65));

// Accept burst time for each process

burstTime[c] = sc.nextInt();

}// End of for loop

// Loops till number of processes to accept arrival time

for(int c = 0; c < numberOfProcess; c++)

{

// Displays process name

System.out.printf(" Enter arrival time for process %c: ", (char)(c + 65));

// Accept arrival time for each process

arrivalTime[c] = sc.nextInt();

}// End of for loop

// Loops till number of processes to create process using constructor

for(int c = 0; c < numberOfProcess; c++)

{

// Stores the process name by adding 65 ASCII value of 'A' with counter value and converts it to character

char ch = (char)(c + 65);

// Creates a process by calling parameterized constructor

process[c] = new Process(ch, burstTime[c], arrivalTime[c]);

}// End of for loop

// Calls the function to find average waiting time and turn around time

findavgTime(process, process.length);

// Closes the scanner class

sc.close();

}// End of main method

}// End of class

Sample Output:

Enter number of process: 10

Enter burst time for process A: 16

Enter burst time for process B: 2

Enter burst time for process C: 11

Enter burst time for process D: 6

Enter burst time for process E: 1

Enter burst time for process F: 9

Enter burst time for process G: 4

Enter burst time for process H: 14

Enter burst time for process I: 1

Enter burst time for process J: 8

Enter arrival time for process A: 0

Enter arrival time for process B: 3

Enter arrival time for process C: 5

Enter arrival time for process D: 9

Enter arrival time for process E: 10

Enter arrival time for process F: 12

Enter arrival time for process G: 14

Enter arrival time for process H: 16

Enter arrival time for process I: 17

Enter arrival time for process J: 19

Processes Burst time Waiting time Turn around time

A 16 42 58

B 2 0 2

C 11 12 23

D 6 1 7

E 1 0 1

F 9 24 33

G 4 3 7

H 14 42 56

I 1 0 1

J 8 9 17

Average waiting time = 13.3

Average turn around time = 20.5

-----------------------------------------------------------------------------

// SRT

import java.util.*;

// Class SRT_CPU_Arrival definition for Shortest Remaining Time CPU Scheduling

public class SRT_CPU_Arrival

{

// main method definition

public static void main(String ss[])

{

// Scanner class object created

Scanner sc = new Scanner(System.in);

// To store the calculated Total Time and initialization of Time Chart array

int totalTime = 0;

// Printing the average Waiting Time and Turn Around Time

float totWT = 0, totTAT = 0;

// Accepts number of processes

System.out.println("Enter number of Processes: ");

int numberOfProcess = sc.nextInt();

// Creates matrix for process number for each row

// Column zero index position for arrival time

// Column one index position for burst time

// Column two index position for waiting time

// Column three index position for turn around time

int process[][] = new int[numberOfProcess + 1][4];

// Loops till number of processes to accept burst time

for(int c = 1; c <= numberOfProcess; c++)

{

// Display process name

System.out.print("Enter Arrival Time for Process " + (char)(c + 64) + ": ");

// Accepts arrival time

process[c][0] = sc.nextInt();

}// End of for loop

// Loops till number of processes to accept burst time

for(int c = 1; c <= numberOfProcess; c++)

{

// Display process name

System.out.print("Enter Burst Time for Process " + (char)(c + 64) + ": ");

// Accepts burst time

process[c][1] = sc.nextInt();

}// End of for loop

// Loops till number of processes

for(int c = 1; c <= numberOfProcess; c++)

// Calculates total time taken

totalTime += process[c][1];

  

// Loops till total time required for all the process

for(int c = 0; c < totalTime; c++)

{

// Selection of shortest process which has arrived and initializes it to zero

int selectProc = 0;

// To keep track of shortest time, initializes to max value

int minimum = Integer.MAX_VALUE;

// Loops till number of processes

for(int d = 1; d <= numberOfProcess; d++)

{

// Condition to check if Process has arrived

if(process[d][0] <= c)

{

// Checks if the current process burst is less than the minimum

// and current process burst time is not zero

if(process[d][1] < minimum && process[d][1] != 0)

{

// Update the minimum to the burst time of the current process

minimum = process[d][1];

// Update the selected process to current process

selectProc = d;

}// End of inner if condition

}// End of outer if condition

}// End of inner for loop

  

// Decrement Remaining Time of selected process by 1 since it has been assigned the CPU for 1 unit of time

process[selectProc][1]--;

  

// Loops till number of processes to calculate waiting time and turn around time

for(int d = 1; d <= numberOfProcess; d++)

{

// Checks if the current process arrival time is less than the time

if(process[d][0] <= c)

{

// Checks if the current process burst time is not zero

if(process[d][1] != 0)

{

// Checks if process has arrived and it has not already completed execution

// its turn around time is incremented by 1

process[d][3]++;

// Checks if the selected process has not been currently assigned the CPU

// and has arrived its waiting time is incremented by 1

if(d != selectProc)

process[d][2]++;

}// End of inner if condition

// Checks if the process has been assigned CPU and has completed its execution

else if(d == selectProc)

// Increase the current process turn around time by one

process[d][3]++;

}// End of outer if condition

}// End of inner for loop   

}// End of outer for loop

// Displays the heading

System.out.print(" Process Waiting Time Turn Around Time ");

// Loops till number of processes

for(int c = 1; c <= numberOfProcess; c++)

{

// Displays each process information

System.out.printf(" %d %2d %2d", (char)(c + 64), process[c][2], process[c][3]);

totWT += process[c][2];

totTAT += process[c][3];

}// End of for loop

  

// Calculates and displays average waiting time and turn around time

System.out.printf(" Average Waiting Time: %.2f", totWT / numberOfProcess);

System.out.printf(" Average Turn Around Time: %.2f", totTAT / numberOfProcess);

// Closes the scanner class

sc.close();

}// End of main method

}// End of class

Sample Output:

Enter number of Processes: 10

Enter Burst Time for Process A: 16

Enter Burst Time for Process B: 2

Enter Burst Time for Process C: 11

Enter Burst Time for Process D: 6

Enter Burst Time for Process E: 1

Enter Burst Time for Process F: 9

Enter Burst Time for Process G: 4

Enter Burst Time for Process H: 14

Enter Burst Time for Process I: 1

Enter Burst Time for Process J: 8

Enter Arrival Time for Process A: 0

Enter Arrival Time for Process B: 3

Enter Arrival Time for Process C: 5

Enter Arrival Time for Process D: 9

Enter Arrival Time for Process E: 10

Enter Arrival Time for Process F: 12

Enter Arrival Time for Process G: 14

Enter Arrival Time for Process H: 16

Enter Arrival Time for Process I: 17

Enter Arrival Time for Process J: 19

Process Waiting Time Turn Around Time

A 42 58

B 0 2

C 12 23

D 1 7

E 0 1

F 24 33

G 3 7

H 42 56

I 0 1

J 9 17

Average Waiting Time: 13.30

Average Turn Around Time: 20.50

------------------------------------------------------------------------------------

// RR

import java.util.Scanner;

// Class RR_CPU definition for Round Robin CPU scheduling

public class RR_CPU

{

// static method to find the waiting time for all processes

static void waitingTime(char process[], int numberOfProcess, int burstTime[], int waitingTime[], int quantum)

{

// Make a copy of burst times burstTime[] to store remaining burst times.

int remainigBurstTime[] = new int[numberOfProcess];

// Loops till number of process

for (int c = 0 ; c < numberOfProcess; c++)

// Creates a copy of burst time of each process

remainigBurstTime[c] = burstTime[c];

  

// Initializes current time to zero

int currentTime = 0;

  

// Keep traversing processes in round robin manner until all of them are not done

while(true)

{

// Initializes done status to true for each process

boolean done = true;   

// Loops till number of process

for (int c = 0 ; c < numberOfProcess; c++)

{

// Checks if burst time of a process is greater than 0 then only need to process further

if (remainigBurstTime[c] > 0)

{

// There is a pending process, so set the done to false

done = false;

// Checks if the current process remaining is greater than time quantum

if (remainigBurstTime[c] > quantum)

{

// Increase the value of currntTime i.e. shows how much time a process has been processed

currentTime += quantum;

  

// Decrease the burst time of current process by quantum

remainigBurstTime[c] -= quantum;

}// End of inner if condition

  

// If burst time is smaller than or equal to quantum. Last cycle for this process

else

{

// Increase the value of currentTime i.e. shows how much time a process has been processed

currentTime = currentTime + remainigBurstTime[c];

  

// Waiting time is current time minus time used by this process

waitingTime[c] = currentTime - burstTime[c];

  

// As the process gets fully completed make its remaining burst time = 0

remainigBurstTime[c] = 0;

}// End of else

}// End of outer if condition

}// End of for loop   

// Checks if all processes are completed

if (done == true)

// Come out of the loop

break;

}// End of while loop

}// End of method

  

// Static method to calculate turn around time

static void turnAroundTime(char processes[], int numberOfProcess, int burstTime[],

int waitingTime[], int turnAroundTime[])

{

// Loops till number of process

for (int c = 0; c < numberOfProcess; c++)

// Calculates turn around time by adding burstTime[c] + waitingTime[c]

turnAroundTime[c] = burstTime[c] + waitingTime[c];

}// End of method

  

// static method to calculate average time

static void avgTime(char processes[], int numberOfProcess, int burstTime[], int quantum)

{

// Creates an array to store waiting time of each process

int waitingTime[] = new int[numberOfProcess];

// Creates an array to store turn around time of each process

int turnAroundTime[] = new int[numberOfProcess];

// To store total waiting time and turn around time and initializes it to zero

int totalWT = 0, totalTAT = 0;

  

// Calls the method to calculate waiting time of all processes

waitingTime(processes, numberOfProcess, burstTime, waitingTime, quantum);

  

// Calls the method to calculate turn around time for all processes

turnAroundTime(processes, numberOfProcess, burstTime, waitingTime, turnAroundTime);

  

// Display heading

System.out.println("Processes " + " Burst time " + " Waiting time " + " Turn around time");

  

// Loops till number of process

for (int c = 0; c < numberOfProcess; c++)

{

// Calculate total waiting time and total waiting time

// by adding total waiting time with waiting time of each process

totalWT = totalWT + waitingTime[c];

// Calculate total turn around time and total turn around time

// by adding total waiting time with turn around time of each process

totalTAT = totalTAT + turnAroundTime[c];

// Displays each process information

System.out.println(" " + (c + 1) + " " + burstTime[c] +" "

+ waitingTime[c] +" " + turnAroundTime[c]);

}// End of for loop

// Calculates and displays average waiting time and turn around time

System.out.println("Average waiting time = " + (float)totalWT / (float)numberOfProcess);

System.out.println("Average turn around time = " + (float)totalTAT / (float)numberOfProcess);

}// End of method

// main method definition

public static void main(String[] args)

{

// Scanner class object created

Scanner sc = new Scanner(System.in);

// Accepts number of processes

System.out.print("Enter number of process: ");

int numberOfProcess = sc.nextInt();

// Creates an array to store process name

char process[] = new char[numberOfProcess];   

// Creates an array to store burst time of all processes

int burstTime[] = new int[numberOfProcess];

// Loops till number of process

for(int c = 0; c < numberOfProcess; c++)

{

// Stores the process name by adding 65 ASCII value of 'A' with counter value and converts it to character

process[c] = (char)(c + 65);

// Displays the message for process name

System.out.printf(" Enter brust time for process %d: ", (c + 1));

// Accepts burst time of each process

burstTime[c] = sc.nextInt();

}// End of for loop

// Accepts time quantum

System.out.print("Enter time quantum: ");

int quantum = sc.nextInt();

// Calls the function to find average waiting time and turn around time

avgTime(process, numberOfProcess, burstTime, quantum);

// Closes the scanner class

sc.close();

}// End of main method

}// End of class

Sample Output:

Enter number of process: 10

Enter brust time for process A: 16

Enter brust time for process B: 2

Enter brust time for process C: 11

Enter brust time for process D: 6

Enter brust time for process E: 1

Enter brust time for process F: 9

Enter brust time for process G: 4

Enter brust time for process H: 14

Enter brust time for process I: 1

Enter brust time for process J: 8

Enter time quantum: 4

Processes Burst time Waiting time Turn around time

A 16 54 70

B 2 4 6

C 11 50 61

D 6 36 42

E 1 14 15

F 9 53 62

G 4 19 23

H 14 58 72

I 1 27 28

J 8 46 54

Average waiting time = 36.1

Average turn around time = 43.3