First Come First Serve (FCFS) Scheduling Algorithm > Java Program

Operating Systems

First Come First Serve (FCFS) Scheduling Algorithm > Java Program


First come first serve (FCFS) scheduling algorithm with this schema the process that request the CPU First is allocated the CPU first.the Implementation of the First come first serve (FCFS) policy is easily managed with fifo queue.when a process enters into the ready queue ,its PCB is linked onto the tail of the queue.when the CPU is free it is allocated to the process at the head of the queue.the running process is then removed from the queue.the code for First come first serve (FCFS) scheduling is simple to write and Understand.The First come first serve (FCFS) sheduling algorithm in non preemptive.The First come first serve (FCFS) algorithm is particularly troublesome for time sharing system. 

import java.io.*; 
class fcfs 

public static void main(String args[]) throws Exception 

int n,AT[],BT[],WT[],TAT[]; 


float AWT=0;
float ATAT=0;
InputStreamReader isr=new InputStreamReader(System.in); 
BufferedReader br=new BufferedReader(isr); 
System.out.println("Enter no of process"); 
n=Integer.parseInt(br.readLine()); 
BT=new int[n]; 
WT=new int[n]; 
TAT=new int[n]; 
AT=new int[n]; 
System.out.println("Enter Burst time for each process\n******************************"); 
for(int i=0;i<n;i++) 

System.out.println("Enter BT for process "+(i+1)); 
BT[i]=Integer.parseInt(br.readLine()); 

System.out.println("***********************************************"); 
for(int i=0;i<n;i++) 

System.out.println("Enter AT for process"+(i+1)); 
AT[i]=Integer.parseInt(br.readLine()); 

System.out.println("***********************************************"); 
WT[0]=0;
for(int i=1;i<n;i++) 

WT[i]=WT[i-1]+BT[i-1]+AT[i-1]; 
WT[i]=WT[i]-AT[i]; 

for(int i=0;i<n;i++)

TAT[i]=WT[i]+BT[i]; 
AWT=AWT+WT[i]; 

System.out.println("  PROCESS   BT      WT      TAT     "); 
for(int i=0;i<n;i++) 
{
System.out.println("    "+ i + "       "+BT[i]+"       "+WT[i]+"       "+TAT[i]);

AWT=AWT/n; 
System.out.println("***********************************************"); 
System.out.println("Avg waiting time="+AWT+"\n***********************************************");

for(int i=0;i<n;i++) 

TAT[i]=WT[i]+BT[i]; 
ATAT=ATAT+TAT[i]; 
}

ATAT=ATAT/n; 
System.out.println("***********************************************"); 
System.out.println("Avg turn around time="+ATAT+"\n***********************************************"); 

}


/* Output for First come first serve (FCFS) scheduling program: 
run:
Enter no of process
5
Enter Burst time for each process
******************************
Enter BT for process 1
4
Enter BT for process 2
5
Enter BT for process 3
6
Enter BT for process 4
2
Enter BT for process 5
1
***********************************************
Enter AT for process1
0
Enter AT for process2
2
Enter AT for process3
4
Enter AT for process4
5
Enter AT for process5
6
***********************************************
  PROCESS   BT      WT      TAT     
    0       4       0       4
    1       5       2       7
    2       6       5       11
    3       2       10       12
    4       1       11       12
***********************************************
Avg waiting time=5.6
***********************************************
***********************************************
Avg turn around time=9.2
***********************************************
BUILD SUCCESSFUL (total time: 26 seconds)
*/

Comments

Popular posts from this blog

Intermediate Code Generation > C Program