Tuesday 14 May 2013

8. SRT SCHEDULING IN JAVA


Code:
import java.util.*;
public class SRT {
public static void main(String args[]){
int table1[][] = new int[5][6];
int table2[][] = new int[5][6];
int total = 0;
boolean con[] = new boolean[5];
boolean fin[] = new boolean[5];
boolean lowest[] = new boolean[5];
Scanner sc = new Scanner(System.in);
for(int a=0;a<5;a++){

System.out.print("Input ARRIVAL TIME and BURST TIME
for process #"+(a+1)+" (separated by space): ");
StringTokenizer st = new StringTokenizer(sc.nextLine());
int b=1;
while(st.hasMoreTokens()){
table1[a][b] = Integer.parseInt(""+st.nextToken());
table2[a][b] = table1[a][b];
b++;
}
table1[a][0] = (a+1);
table2[a][0] = table1[a][0];
total += table1[a][2];
con[a] = false;
fin[a] = false;
lowest[a] = false;
}
String str[] = new String[total];
for(int c=0;c<total;c++){
for(int d=0;d<5;d++){
if(table1[d][1]<=c&&!fin[d]){
con[d] = true;
}
}
int low=0;
for(int j=0;j<5;j++){
if(con[j]){
low = table1[j][2];
break;
}
}
for(int k=0;k<5;k++){
if(table1[k][2]<low&&con[k]){
low = table1[k][2];
}
}
for(int l=0;l<5;l++){
if(table1[l][2]==low){
lowest[l] = true;
break;
}
}
for(int f=0;f<5;f++){
if(lowest[f]){
table1[f][2] -= 1;
if(table1[f][2]==0){
fin[f] = true;
str[c] = ""+(f+1);
break;
}
}
}
if(str[c]==null) str[c] = "0";
for(int p=0;p<5;p++){
lowest[p] = false;
con[p] = false;
}
}
for(int n=0;n<total;n++){
if(str[n]!="0"){
table2[Integer.parseInt(str[n])-1][5] = n+1;
}
}
for(int t=0;t<5;t++){
table2[t][4] = table2[t][5] - table2[t][1];
table2[t][3] = table2[t][4] - table2[t][2];
}
System.out.println("\nP\tAT\tBT\tWT\tTT\tET");
for(int x=0;x<5;x++){
System.out.println(""+table2[x][0]+"\t"+table2[x][1]+"\t"+table2[x][2]+"\t"
+table2[x][3]+"\t"+table2[x][4]+"\t"+table2[x][5]+"");
AWT=AWT+table2[x][2];
ATT=ATT+table2[x][3];
}
System.out.println(“Average waiting time:”+AWT/5);
System.out.println(“Average turnaround time:”+ATT/5);
}
}


OUTPUT:

Input ARRIVAL TIME and BURST TIME for process #1 (separated by space): 0 10
Input ARRIVAL TIME and BURST TIME for process #2 (separated by space): 2 5
Input ARRIVAL TIME and BURST TIME for process #3 (separated by space): 1 12
Input ARRIVAL TIME and BURST TIME for process #4 (separated by space): 3 9
Input ARRIVAL TIME and BURST TIME for process #5 (separated by space): 0 6
P AT BT WT TT ET
1 0 10 20 30 30
2 2 5 -1 4 6
3 1 12 29 41 42
4 3 9 8 17 20
5 0 6 5 11 11
Average waiting time:12.2
Average turnaround time:16.6

No comments:

Post a Comment