-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFCFS and SJF.java
88 lines (75 loc) · 1.71 KB
/
FCFS and SJF.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// program to simulate CPU Scheduling Algorithms: FCFS and SJF: (Using Switch Case):-
import java.util.*;
public class Main{
public static void main(String args[]){
int wt[],proc[],tat[],bst[],n,i,j,total=0;
Scanner sc= new Scanner(System.in);
System.out.println("Page scheduling MENU: ");
System.out.println(" 1. Using FCFS?");
System.out.println(" 2. Using SJF?");
System.out.print("Your Choice==> ");
int x= sc.nextInt();
System.out.print("\nNo. of processes: ");
n=sc.nextInt();
proc = new int[n];
wt=new int[n];
bst=new int[n];
tat= new int[n];
switch (x){
case 1:
System.out.println("Enter Cpu time: ");
for(i=0;i<n;i++){
System.out.print(" Process["+(i+1)+"]: ");
bst[i]=sc.nextInt();
proc[i]=i+1;
}
wt[0]=0;
for(i = 1;i<n;i++){
wt[i]=0;
for(j=0;j<i;j++){
wt[i]+=bst[j];
total+=wt[i];
}
}
System.out.println("\nProcess\t\tBT\tWT\tTAT");
System.out.println("-------------------------------------");
for(i=0;i<n;i++){
tat[i]=wt[i]+bst[i];
System.out.println("Proc["+proc[i]+"]\t\t"+bst[i]+"\t"+wt[i]+"\t"+tat[i]);
}
case 2:
System.out.println("Enter Cpu time: ");
for(i=0;i<n;i++){
System.out.print(" Process["+(i+1)+"]: ");
bst[i]=sc.nextInt();
proc[i]=i+1;
}
for(i=0;i<n;i++){
int pp=i;
for(j=i+1;j<n;j++){
if(bst[j]<bst[pp])
pp=j;}
int temp=bst[i];
bst[i]=bst[pp];
bst[pp]=temp;
temp=proc[i];
proc[i]=proc[pp];
proc[pp]=temp;
}
wt[0]=0;
for(i = 1;i<n;i++){
wt[i]=0;
for(j=0;j<i;j++){
wt[i]+=bst[j];
total+=wt[i];
}
}
System.out.println("\nProcess\t\tBT\tWT\tTAT");
System.out.println("-------------------------------------");
for(i=0;i<n;i++){
tat[i]=wt[i]+bst[i];
System.out.println("Proc["+proc[i]+"]\t\t"+bst[i]+"\t"+wt[i]+"\t"+tat[i]);
}
}
}
}