-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcfs_sjf.java
More file actions
130 lines (112 loc) · 3.28 KB
/
Copy pathfcfs_sjf.java
File metadata and controls
130 lines (112 loc) · 3.28 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package sposlab;
import java.util.*;
public class fcfs_sjf {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
System.out.println("********* Menu *********");
System.out.println("1. fcfs \n2. sjf" );
System.out.print("enter a button 1 or 2 :- ");
int a=sc.nextInt();
switch(a) {
case 1:
System.out.println("************ fcfs *************");
int bst[],process[],wt[],tat[],i,j,n,total=0,pos,temp;
float wt_avg, tat_avg;
System.out.print("Enter number of process :- ");
n=sc.nextInt();
process=new int[n];
bst=new int[n];
wt=new int[n];
tat=new int[n];
System.out.println("Enter cpu number");
for(i=0;i<n;i++) {
System.out.print("\n process["+(i+1)+"]: ");
bst[i]=sc.nextInt();
process[i]=i+1;
}
System.out.println("\t\t\t**********FCFS Scheduling*********");
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];
}
wt_avg=(float)total/n;
total=0;
System.out.println("-----------------------------------------------------------------------");
System.out.println("\nProcess\t\t| Burst Time \t\t|Waiting Time\t\t|Turn Time");
System.out.println("-----------------------------------------------------------------------");
for(i=0;i<n;i++)
{
tat[i]=bst[i]+wt[i];
total+=tat[i];
System.out.println("\np"+process[i]+"\t\t|\t"+bst[i]+"\t\t|\t"+wt[i]+"\t\t|\t"+tat[i]);
System.out.println("-----------------------------------------------------------------------");
}
tat_avg=(float)total/n;
System.out.println("\n\nAverage Waiting Time: "+wt_avg);
System.out.println("\nAverage Turnaround Time: "+tat_avg);
break;
case 2:
System.out.println("********** sjf ***********");
int tot=0;
System.out.print("Enter number of process: ");
n = sc.nextInt();
process = new int[n];
bst= new int[n];
wt= new int[n];
tat = new int[n];
System.out.println("\nEnter Burst time:");
for(i=0;i<n;i++)
{
System.out.print("\nProcess["+(i+1)+"]: ");
bst[i] = sc.nextInt();;
process[i]=i+1;
}
System.out.println("\n \t \t*************** Shortest Job First Scheduling*********");
for(i=0;i<n;i++)
{
int pp=i;
for(j=i+1;j<n;j++)
{
if(bst[j]<bst[pp])
pp=j;
}
temp=bst[i];
bst[i]=bst[pp];
bst[pp]=temp;
temp=process[i];
process[i]=process[pp];
process[pp]=temp;
}
//First process has 0 waiting time
wt[0]=0;
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bst[j];
tot+=wt[i];
}
wt_avg=(float)tot/n;
tot=0;
System.out.println("----------------------------------------------------------");
System.out.println("\nProcess\t| Burst Time \t|Waiting Time\t|Turnaround Time |");
System.out.println("----------------------------------------------------------");
for(i=0;i<n;i++)
{
tat[i]=bst[i]+wt[i];
tot+=tat[i];
System.out.println("\n p"+process[i]+"\t|\t "+bst[i]+"\t|\t "+wt[i]+"\t|\t"+tat[i]+"\t|\t ");
System.out.println("----------------------------------------------------------");
}
tat_avg=(float)tot/n;
System.out.println("\n\nAverage Waiting Time: "+wt_avg);
System.out.println("\nAverage Turnaround Time: "+tat_avg);
break;
}
}
}