Assignment No.
3
UCE2023431
Title: Simulation of the scheduling algorithms. For example: First Come
First Serve
(FCFS), Shortest Job First (SJF).
Objective: To implement non-pre-emptive CPU scheduling algorithms.
Code:
import java.util.*;
class PCB{
String n;
int arrivalTime, burstTime, completionTime, waitingTime,
turnaroundTime;
PCB(String n, int at, int bt) {
this.n = n;
arrivalTime = at;
burstTime = bt;
waitingTime = 0;
turnaroundTime = 0;
}
void calcTTWT(PCB process) {
process.turnaroundTime=process.completionTime-process.arrivalTim
e;
process.waitingTime=process.turnaroundTime-process.burstTime;
}
}
public class Main {
static Scanner sc = new Scanner(System.in);
static void sortArr(PCB[] jobQueue) {
Arrays.sort(jobQueue, (p1, p2) ->
Integer.compare(p1.arrivalTime, p2.arrivalTime));
}
static void FCFS(PCB[] jobQueue) {
sortArr(jobQueue);
for (int i = 0; i < jobQueue.length; i++) {
if (i == 0) {
jobQueue[i].completionTime =
jobQueue[i].arrivalTime+ jobQueue[i].burstTime;
} else {
//ct of prev + bt of curr
jobQueue[i].completionTime =
jobQueue[i].burstTime + jobQueue[i-1].completionTime;
}
jobQueue[i].calcTTWT(jobQueue[i]);
}
}
static void SJF(PCB[] jobQueue) {
sortArr(jobQueue);
PriorityQueue<PCB> readyQueue = new
PriorityQueue<>(Comparator.comparingInt(p -> p.burstTime));
// sort comparator burst time
int currentTime = 0;
int i = 0;
while (i < jobQueue.length || !readyQueue.isEmpty()) {
// processes waiting in the ready ques or till all the process
in job queue not completed
while (i < jobQueue.length &&
jobQueue[i].arrivalTime <= currentTime) {
readyQueue.add(jobQueue[i]); // PQ will sort it
during insertion
i++;
}
if (readyQueue.isEmpty()) { // no process waiting
to be executed
if (i < jobQueue.length) {
currentTime = jobQueue[i].arrivalTime;
} else {
break;
}
continue; // readyQueue is empty here so
polling of the same should not take place
}
PCB p = readyQueue.poll();
p.completionTime = currentTime + p.burstTime;
currentTime = p.completionTime;
p.calcTTWT(p);
}
}
static void SRTF(PCB[] jobQueue) {
sortArr(jobQueue);
int[] remainingBurstTime = new int[jobQueue.length];
for (int i = 0; i < jobQueue.length; i++) {
remainingBurstTime[i] = jobQueue[i].burstTime;
}
int currentTime = 0;
int cp = 0; //completed processes count
PriorityQueue<Integer> readyQueue = new
PriorityQueue<>(Comparator.comparingInt(i ->
remainingBurstTime[i]));
// sort comparator remaining burst time
int i = 0;
while (cp < jobQueue.length) {
while (i < jobQueue.length &&
jobQueue[i].arrivalTime <= currentTime) {
readyQueue.add(i);
i++;
}
if (readyQueue.isEmpty()) {
currentTime++;
continue;
}
int p = readyQueue.poll();
remainingBurstTime[p]--;
currentTime++;
if (remainingBurstTime[p] == 0) {
cp++;
jobQueue[p].completionTime = currentTime;
jobQueue[p].calcTTWT(jobQueue[p]);
} else {
readyQueue.add(p);
}
}
}
static void RR(PCB[] jobQueue, int timeQuantum) {
sortArr(jobQueue);
Queue<Integer> q = new LinkedList<>(); // q of indices
of the processes of jobQueue
int currentTime = 0;
int[] remainingBurstTime = new int[jobQueue.length];
for (int i = 0; i < jobQueue.length; i++) {
remainingBurstTime[i] = jobQueue[i].burstTime;
}
int i = 0;
while (i < jobQueue.length && jobQueue[i].arrivalTime
<= currentTime) {
q.add(i);
i++;
}
while (!q.isEmpty()) {
int p = q.poll();
if (remainingBurstTime[p] > timeQuantum) {
currentTime += timeQuantum;
remainingBurstTime[p] -= timeQuantum;
} else {
currentTime += remainingBurstTime[p];
jobQueue[p].completionTime = currentTime;
jobQueue[p].calcTTWT(jobQueue[p]);
remainingBurstTime[p] = 0;
}
while (i < jobQueue.length &&
jobQueue[i].arrivalTime <= currentTime) {
q.add(i);
i++;
}
if (remainingBurstTime[p] > 0) {
q.add(p);
}
}
}
static void displayA(PCB[] processes) {
int ttsum = 0, wtsum = 0;
for (int i = 0; i < processes.length; i++) {
System.out.println(processes[i].n + " " +
processes[i].arrivalTime + " " + processes[i].burstTime + " "
+ processes[i].completionTime + " " +
processes[i].waitingTime + " " + processes[i].turnaroundTime);
ttsum = ttsum + processes[i].turnaroundTime;
wtsum = wtsum + processes[i].waitingTime;
}
System.out.println("Average turn around time: " +
ttsum / processes.length);
System.out.println("Average waiting around time: " +
wtsum / processes.length);
}
public static void main(String[] args) {
System.out.print("Enter the number of processes: ");
int p = sc.nextInt();
PCB[] jobQueue = new PCB[p];
int at, bt;
String n;
for (int i = 0; i < p; i++) {
sc.nextLine();
System.out.print("Enter the name of the process:
");
n = sc.nextLine();
System.out.print("Enter the arrival time: ");
at = sc.nextInt();
System.out.print("Enter the burst time: ");
bt = sc.nextInt();
jobQueue[i] = new PCB(n, at, bt);
System.out.println();
}
int ch=0;
do {
System.out.print("Enter your choice=> 1. FCFS
2. SJF 3. SRTF 4.RR : ");
ch=sc.nextInt();
switch(ch) {
case 1: //FCFS
FCFS(jobQueue);
displayA(jobQueue);
break;
case 2: //SJF
SJF(jobQueue);
displayA(jobQueue);
break;
case 3: //SRTF
SRTF(jobQueue);
displayA(jobQueue);
break;
case 4: //RR
System.out.print("Enter the time
quantum: ");
int q= sc.nextInt();
RR(jobQueue,q);
displayA(jobQueue);
break;
case 0: //exit
System.out.println("Exited");
break;
default: System.out.println("Invalid
choice!");
}
} while (ch != 0);
}
}