SJF shortest job first scheduling algorithm program in java

A different approach to CPU scheduling is SJF shortest job first scheduling algorithm.This associates with each process the length of the latter next CPU burst.When the CPU is available it is assigned to the process that has the smallest next CPU burst.if two processes have same length next CPU burst,FCFS scheduling is used to break the tie.Note that a more appropriate term would be the shortest next CPU burst ,because the scheduling is done by examining the length of the next CPU burst because the scheduling is done by examining the length of the next CPU burst of a process,rather than its total length.The real difficulty in SJF shortest job first scheduling algorithm is knowing the length of the next process.SJF shortest job first scheduling algorithm scheduling algorithm is provably optimal.in that it gives the minimum average waiting time for a given set of processes.

SJF shortest job first scheduling algorithm Program :


import java.util. * ;
class SJF {
public static void main(String args[]) {
Scanner sc = new Scanner(System. in );
int n,
BT[],
WT[],
TAT[];
System.out.println("Enter no of process");
n = sc.nextInt();
BT = new int[n + 1];
WT = new int[n + 1];
TAT = new int[n + 1];
float AWT = 0;
System.out.println("Enter Burst time for each process");
for (int i = 0; i < n; i++) {
System.out.println("Enter BT for process " + (i + 1));
BT[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
WT[i] = 0;
TAT[i] = 0;
}
int temp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (BT[j] > BT[j + 1]) {
temp = BT[j];
BT[j] = BT[j + 1];
BT[j + 1] = temp;
temp = WT[j];
WT[j] = WT[j + 1];
WT[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
TAT[i] = BT[i] + WT[i];
WT[i + 1] = TAT[i];
}
TAT[n] = WT[n] + BT[n];
System.out.println(" PROCESS BT WT TAT ");
for (int i = 0; i < n; i++)
System.out.println(" " + i + " " + BT[i] + " " + WT[i] + " " + TAT[i]);
for (int j = 0; j < n; j++)
AWT += WT[j];
AWT = AWT / n;
System.out.println("***********************************************");
System.out.println("Avg waiting time=" + AWT + "\n***********************************************");
}
}

/*
O/P for SJF shortest job first scheduling algorithm is :

Enter no of process
4
Enter Burst time for each process
Enter BT for process 1
5
Enter BT for process 2
3
Enter BT for process 3
3
Enter BT for process 4
1
PROCESS BT WT TAT
0 1 0 1
1 3 1 4
2 3 4 7
3 5 7 12
***********************************************
Avg waiting time=3.0
***********************************************
*/

[purchase_link id="1286" style="button" color="blue" text="Download SJF Program"]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *