Wednesday, 14 January 2015

CPU Scheduling algorithm - FCFS (Java)


This is a Java program which implements one of the CPU Scheduling algorithm called First Come First Served(FCFS).

FCFS is the simplest scheduling algorithm , easy to understand and implement but not as efficient as remaining scheduling algorithms as its waiting time is high.

As the name suggests , here the processes/jobs are executed on first come first serve basis.




PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class FCFS {
 public static void main(String[] args) {
  int i,n;
     float avgWt,totalWt=0;
     Scanner scan = new Scanner(System.in);
     System.out.print("Enter how many jobs ?\t");
     n = scan.nextInt();
     int bt[]= new int[n];
     int wt[]= new int[n];
     for(i=0;i<n;i++)
     {
      System.out.print("Enter burst time for job "+(i+1)+" :\t");
      bt[i] = scan.nextInt();
     }
     scan.close();
     System.out.print("\n\nWaiting time for Job 1 : 0 units\t");
         wt[0]=0;
     for(i=1;i<n;i++)
     {
         wt[i]=bt[i-1]+wt[i-1];
         System.out.print("\nWaiting time for Job"+(i+1)+" : "+wt[i]+" units \t");
             totalWt = totalWt + wt[i];
     }
     System.out.print("\n\nThe total waiting time : "+totalWt);
     avgWt= totalWt/n;
     System.out.println("\n\nAverage waiting time : "+avgWt);
 }
}
NOTE : Assuming that all jobs arrive at the same time.

OUTPUT : 
Java - CPU Scheduling Algorithm(FCFS)