SJF Scheduling Algorithm Java Implementation.

//Java program for Shortest Job First Scheduling algorithm.

import java.io.*;
public class SJF{
 public static void main(String args[]) throws IOException
 {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int n;
      System.out.println("Please enter the number of Processes: ");
       n = Integer.parseInt(br.readLine());
       int proc[][] = new int[n + 1][4];
       for(int i = 1; i <= n; i++)
       {
      System.out.println("Please enter the Arrival Time for Process " + i + ": ");
      proc[i][0] = Integer.parseInt(br.readLine());
      System.out.println("Please enter the Burst Time for Process " + i + ": ");
      proc[i][1] = Integer.parseInt(br.readLine());
     }
       System.out.println();
     
      
     int total_time = 0;
     for(int i = 1; i <= n; i++)
     {
      total_time += proc[i][1];
     }
     int time_chart[] = new int[total_time];
     
     for(int i = 0; i < total_time; i++)
     {
    
      int sel_proc = 0;
      int min = 99999;
      for(int j = 1; j <= n; j++)
      {
       if(proc[j][0] <= i)
       {
        if(proc[j][1] < min && proc[j][1] != 0)
        {
         min = proc[j][1];
         sel_proc = j;
        }
       }
      }
      
     
      time_chart[i] = sel_proc;
      
      
      proc[sel_proc][1]--;
      
      
      for(int j = 1; j <= n; j++)
      {
       if(proc[j][0] <= i)
       {
        if(proc[j][1] != 0)
        {
         proc[j][3]++;
            if(j != sel_proc)
             proc[j][2]++;
        }
        else if(j == sel_proc)
         proc[j][3]++;
       }
      }
      
     
      if(i != 0)
      {
       if(sel_proc != time_chart[i - 1])
       
       {
        System.out.print("--" + i + "--P" + sel_proc);
       }
      }
      else
       System.out.print(i + "--P" + sel_proc);
      if(i == total_time - 1)
       System.out.print("--" + (i + 1));
      
     }
     System.out.println();
     System.out.println();
     
     
     System.out.println("P\t WT \t TT ");
     for(int i = 1; i <= n; i++)
     {
      System.out.printf("%d\t%2dms\t%2dms",i,proc[i][2],proc[i][3]);
      System.out.println();
     }
     
     System.out.println();
     
    
     float WT = 0,TT = 0;
     for(int i = 1; i <= n; i++)
     {
      WT += proc[i][2];
      TT += proc[i][3];
     }
     WT /= n;
     TT /= n;
     System.out.println("The Average WT is: " + WT + "ms");
     System.out.println("The Average TT is: " + TT + "ms");
 }
    
}

/*

OUTPUT:

Please enter the number of Processes: 
4
Please enter the Arrival Time for Process 1: 
0
Please enter the Burst Time for Process 1: 
7
Please enter the Arrival Time for Process 2: 
3
Please enter the Burst Time for Process 2: 
3
Please enter the Arrival Time for Process 3: 
5
Please enter the Burst Time for Process 3: 
2
Please enter the Arrival Time for Process 4: 
4
Please enter the Burst Time for Process 4: 
5

0--P1--3--P2--6--P3--8--P1--12--P4--17

P  WT   TT 
1  5ms 12ms
2  0ms  3ms
3  1ms  3ms
4  8ms 13ms

The Average WT is: 3.5ms
The Average TT is: 7.75ms
*/

Comments

Post a Comment

Popular posts from this blog

Priority Scheduling Algorithm Java Program.

Implement UNIX system calls like ps, fork, join, exec family, and wait for process management (use shell script/ Java/ C programming).

Implement a class CppArray which is identical to a one-dimensional C++ array (i.e., the index set is a set of consecutive integers starting at 0) except for the following : 1. It performs range checking. 2.It allows one to be assigned to another array through the use of assignment operator. 3.It supports a function that returns the size of the array.