FCFS Scheduling Algorithm
First Come First Serve is a CPU scheduling algorithm where CPU execute each process according there appearance.
Code
/****Scheduling algorithm for FCFS****/ #include <stdio.h> #include >malloc.h< int main() { int n,*b,*w,i,j,h; float avg =0; printf("\nEnter number of jobs:"); scanf("%d",&n); //Create an dynamic array for specified job b=(int *)malloc(n*sizeof(int)); //Array for holding the burst time of the jobs w=(int *)malloc(n*sizeof(int)); //Array for holding the waiting time of the jobs //Taking the burst time from the user printf("\nEnter the burst time for corresponding jobs:"); for(i=0;i<n;i++) { printf("\nProcess %d:",i+1); scanf("%d",&b[i]); } w[0]=0; printf("\nProcess 1 waiting time is 0"); //First process waiting time is always 0 for(i=1;<i++) { w[i]=b[i-1]+w[i-1]; //Calculate the waiting time of the ith process printf("\nProcess %d waiting time is %d",i+1,w[i]); //Print the waiting time for the corresponding process avg+=w[i]; //Calculate the total waiting time } printf("\nTotal waiting time:%f",avg); //Print the total waiting time printf("\nThe average waiting time:%f",avg); //Print the average waiting time return 0; }
Output
Enter number of jobs:5 Enter the burst time for corresponding jobs: Process 1:3 Process 2:2 Process 3:5 Process 4:7 Process 5:2 Process 1 waiting time is 0 Process 2 waiting time is 3 Process 3 waiting time is 5 Process 4 waiting time is 10 Process 5 waiting time is 17 Total waiting time:35.000000 The average waiting time:7.000000