-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFCFS_CPU_Scheduling.java
More file actions
89 lines (77 loc) · 2.38 KB
/
FCFS_CPU_Scheduling.java
File metadata and controls
89 lines (77 loc) · 2.38 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
package java_algo;
import java.util.*;
public class FCFS_CPU_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]; // process ids
int ar[] = new int[n]; // arrival times
int bt[] = new int[n]; // burst or execution times
int ct[] = new int[n]; // completion times
int ta[] = new int[n]; // turn around times
int wt[] = new int[n]; // waiting times
int temp;
float avgwt = 0, avgta = 0;
for (int i = 0; i < n; i++) {
System.out.println("enter process " + (i + 1) + " arrival time: ");
ar[i] = sc.nextInt();
System.out.println("enter process " + (i + 1) + " brust time: ");
bt[i] = sc.nextInt();
pid[i] = i + 1;
}
//sorting according to arrival times
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - (i + 1); j++) {
if (ar[j] > ar[j + 1]) {
temp = ar[j];
ar[j] = ar[j + 1];
ar[j + 1] = temp;
temp = bt[j];
bt[j] = bt[j + 1];
bt[j + 1] = temp;
temp = pid[j];
pid[j] = pid[j + 1];
pid[j + 1] = temp;
}
}
}
// finding completion times
for (int i = 0; i < n; i++) {
if (i == 0) {
ct[i] = ar[i] + bt[i];
} else {
if (ar[i] > ct[i - 1]) {
ct[i] = ar[i] + bt[i];
} else
ct[i] = ct[i - 1] + bt[i];
}
ta[i] = ct[i] - ar[i]; // turnaround time= completion time- arrival time
wt[i] = ta[i] - bt[i]; // waiting time= turnaround time- burst time
avgwt += wt[i]; // total waiting time
avgta += ta[i]; // total turnaround time
}
System.out.println("\npid arrival brust complete turn waiting");
for (int i = 0; i < n; i++) {
System.out.println(pid[i] + " \t " + ar[i] + "\t" + bt[i] + "\t" + ct[i] + "\t" + ta[i] + "\t" + wt[i]);
}
sc.close();
// Draw Gantt Chart
System.out.println("Gantt Chart : ");
for (int i = 0; i <= n; i++) {
System.out.print(" ------");
}
System.out.println();
for (int i = 0; i < n; i++) {
System.out.print("| P" + pid[i] + " ");
}
System.out.print("|");
System.out.println();
System.out.print(0 + "------");
for (int i = 0; i < n; i++) {
System.out.print(ct[i] + "-----");
}
System.out.println("\naverage waiting time: " + (avgwt / n)); // printing average waiting time.
System.out.println("average turnaround time:" + (avgta / n)); // printing average turnaround time.
}
}