-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreemtiveSJF.java
More file actions
64 lines (55 loc) · 2.87 KB
/
PreemtiveSJF.java
File metadata and controls
64 lines (55 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
class PreemtiveSJF{
int burstTime, arrivalTime, process, turnaroundTime, waitingTime, copyOfBurstTime;
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Number of Processes : ");
int numberOfProcesses=scanner.nextInt();
PreemtiveSJF object[] = new PreemtiveSJF[numberOfProcesses+1];
for(int i = 0; i <= numberOfProcesses; i++)
object[i]= new PreemtiveSJF();
int sum=0;
for(int m = 1; m <= numberOfProcesses ; m++){
object[m].process = m;
System.out.println("Enter Burst Time and Arrival Time of Process "+m);
object[m].burstTime = scanner.nextInt();
object[m].arrivalTime = scanner.nextInt();
sum+=object[m].burstTime;
object[m].copyOfBurstTime = object[m].burstTime;
System.out.println(object[m].copyOfBurstTime);
}
System.out.println("\n\nProcess \t Arrival Time \t CPU Burst");
for(int i = 1; i <= numberOfProcesses; i++){
System.out.println(""+(i)+" \t\t "+object[i].arrivalTime+" \t\t "+object[i].burstTime);
}
System.out.println();
for(int clockTime = 1; clockTime <= sum; clockTime++){
int min = 9999, index = 0;
for(int j = 1; j <= numberOfProcesses; j++){
if(object[j].arrivalTime < clockTime && object[j].burstTime < min && object[j].burstTime>0){
min=object[j].burstTime;
index=j;
}
}
object[index].burstTime -= 1;
if(object[index].burstTime == 0){
object[index].turnaroundTime=clockTime-object[index].arrivalTime;
object[index].waitingTime=object[index].turnaroundTime-object[index].copyOfBurstTime;
}
System.out.print("| "+object[index].process);
}
int totalTurnaroundTime = 0, totalWaitingTime = 0;
for(int i = 1; i <= numberOfProcesses; i++){
totalTurnaroundTime += object[i].turnaroundTime;
totalWaitingTime += object[i].waitingTime;
}
float averageTurnaroundTime = (float)totalTurnaroundTime/numberOfProcesses;
float averageWaitingTime = (float)totalWaitingTime/numberOfProcesses;
System.out.println("\n\nProcess \t Arrival Time \t Waiting Time \t Turn Around Time");
for(int i = 1; i <= numberOfProcesses; i++){
System.out.println(""+(i)+" \t\t "+object[i].arrivalTime+"\t\t"+object[i].waitingTime+" \t\t "+object[i].turnaroundTime);
}
System.out.println("\n\nAverage Turn Around Time: "+averageTurnaroundTime);
System.out.println("Average Waiting Time: "+averageWaitingTime);
}
}