-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSJF_SchedulingPre.java
71 lines (63 loc) · 2.78 KB
/
SJF_SchedulingPre.java
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
65
66
67
68
69
70
71
import java.util.*;
public class SJF_SchedulingPre {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no of process:");
int totalProcesses = sc.nextInt();
int pid[] = new int[totalProcesses];
int arrivalTime[] = new int[totalProcesses];
int burstTime[] = new int[totalProcesses];
int completionTime[] = new int[totalProcesses];
int turnaroundTime[] = new int[totalProcesses];
int waitingTime[] = new int[totalProcesses];
int flag[] = new int[totalProcesses];
int TempBurst[] = new int[totalProcesses];
int i, executedTime = 0, executedProcesses = 0;
float averageWaitingTime = 0, averageTurnaroundTime = 0;
for (i = 0; i < totalProcesses; i++) {
pid[i] = i + 1;
System.out.println("Enter process " + (i + 1) + " arrival time:");
arrivalTime[i] = sc.nextInt();
System.out.println("Enter process " + (i + 1) + " burst time:");
burstTime[i] = sc.nextInt();
TempBurst[i] = burstTime[i];
flag[i] = 0;
}
while (true) {
int min = 99, c = totalProcesses;
if (executedProcesses == totalProcesses)
break;
for (i = 0; i < totalProcesses; i++) {
if ((arrivalTime[i] <= executedTime) && (flag[i] == 0) && (burstTime[i] < min)) {
min = burstTime[i];
c = i;
}
}
if (c == totalProcesses)
executedTime++;
else {
burstTime[c]--;
executedTime++;
if (burstTime[c] == 0) {
completionTime[c] = executedTime;
flag[c] = 1;
executedProcesses++;
}
}
}
for (i = 0; i < totalProcesses; i++) {
turnaroundTime[i] = completionTime[i] - arrivalTime[i];
waitingTime[i] = turnaroundTime[i] - TempBurst[i];
averageWaitingTime += waitingTime[i];
averageTurnaroundTime += turnaroundTime[i];
}
System.out.println("pid arrival burst complete turn waiting");
for (i = 0; i < totalProcesses; i++) {
System.out
.println(pid[i] + "\t" + arrivalTime[i] + "\t" + TempBurst[i] + "\t" + completionTime[i] + "\t" + turnaroundTime[i] + "\t" + waitingTime[i]);
}
System.out.println("\nAverage Turnaround Time is " + (float) (averageTurnaroundTime / totalProcesses));
System.out.println("Average Waiting Time is " + (float) (averageWaitingTime / totalProcesses));
sc.close();
}
}