-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPre_SJF_Scheduling.java
More file actions
108 lines (90 loc) · 2.41 KB
/
Pre_SJF_Scheduling.java
File metadata and controls
108 lines (90 loc) · 2.41 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package java_algo;
import java.util.*;
public class Pre_SJF_Scheduling {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("enter no of process:");
int n = sc.nextInt();
int pid[] = new int[n]; // it takes pid of process
int at[] = new int[n]; // at means arrival time
int bt[] = new int[n]; // bt means burst time
int ct[] = new int[n]; // ct means complete time
int ta[] = new int[n];// ta means turn around time
int wt[] = new int[n]; // wt means waiting time
int f[] = new int[n]; // f means it is flag it checks process is completed or not
int k[] = new int[n]; // it is also stores brust time
int i, st = 0, tot = 0;
float avgwt = 0, avgta = 0;
for (i = 0; i < n; i++) {
pid[i] = i + 1;
System.out.println("enter process " + (i + 1) + " arrival time:");
at[i] = sc.nextInt();
System.out.println("enter process " + (i + 1) + " burst time:");
bt[i] = sc.nextInt();
k[i] = bt[i];
f[i] = 0;
}
while (true) {
int min = 99, c = n;
if (tot == n)
break;
for (i = 0; i < n; i++) {
if ((at[i] <= st) && (f[i] == 0) && (bt[i] < min)) {
min = bt[i];
c = i;
}
}
if (c == n)
st++;
else {
bt[c]--;
st++;
if (bt[c] == 0) {
ct[c] = st;
f[c] = 1;
tot++;
}
}
}
for (i = 0; i < n; i++) {
ta[i] = ct[i] - at[i];
wt[i] = ta[i] - k[i];
avgwt += wt[i];
avgta += ta[i];
}
System.out.println("pid arrival burst complete turn waiting");
for (i = 0; i < n; i++) {
System.out.println(pid[i] + "\t" + at[i] + "\t" + k[i] + "\t" + ct[i] + "\t" + ta[i] + "\t" + wt[i]);
}
System.out.println("Gantt Chart : ");
int temp = 0;
for (int i1 = 0; i1 < ct.length; i1++) {
for (int j = i1 + 1; j < ct.length; j++) {
if (ct[i1] > ct[j]) {
temp = ct[i1];
ct[i1] = ct[j];
ct[j] = temp;
temp = pid[i1];
pid[i1] = pid[j];
pid[j] = temp;
}
}
}
for (int i1 = 0; i1 <= n; i1++) {
System.out.print(" ------");
}
System.out.println();
for (int i1 = 0; i1 < n; i1++) {
System.out.print("| P" + pid[i1] + " ");
}
System.out.print("|");
System.out.println();
System.out.print(0 + "------");
for (int i1 = 0; i1 < n; i1++) {
System.out.print(ct[i1] + "-----");
}
System.out.println("\naverage tat is " + (float) (avgta / n));
System.out.println("average wt is " + (float) (avgwt / n));
sc.close();
}
}