First Come First Serve (FCFS) CPU Scheduling Algorithm in Operating Systems
First Come First Serve (FCFS) - CPU Scheduling Algorithm
what is FCFS CPU Scheduling Algorithm ?
First Come First Serve (FCFS) is a scheduling algorithm that automatically executes queued requests and processes in the order they arrive.
Source Code
#include <stdio.h>
void main()
{
int i=0,pid[10],at[20],ct[20],bt[20],tat[20],wt[20],n=0,j=0;
float avgw=0,avgtat = 0;
printf("Enter the number of proccess : ");
scanf("%d",&n);
printf("Enter details of each process :\n");
for(i=0; i<n; i++)
{
printf("Enter the proccess id :");
scanf("%d",&pid[i]);
printf("Enter arrival time : ");
scanf("%d",&at[i]);
printf("Enter Burst Time : ");
scanf("%d",&bt[i]);
printf("\n");
}
//Sort the process based on arrival time using bubble sort
int temp =0;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-1; j++)
{
if(at[j] > at[j+1])
{
//swap arrival time
temp = at[j];
at[j] = at[j+1];
at[j+1] = temp;
//swap burst time
temp = bt[j];
bt[j] = bt[j+1];
bt[j+1] = temp;
//swap process ID
temp = pid[j];
pid[j] = pid[j+1];
pid[j+1] = temp;
}
}
}
//calculate completion time for each procces
ct[0] = 0;
for(i=0; i<n; i++)
{
ct[i+1] = ct[i] + bt[i];
}
//calculate turn arround time ,waiting time for each process
for(i=0; i<n; i++)
{
tat[i] = ct[i+1] - at[i];
wt[i] = tat[i] - bt[i];
}
for(i=0;i<n;i++)
{
avgw = avgw + wt[i];
avgtat = avgtat + tat[i];
}
avgw = avgw/n;
avgtat = avgtat/n;
printf("\tPID\tAT\tBT\tCT\tWT\tTAT\n");
for(i=0;i<n;i++)
{
printf("\t%d\t%d\t%d\t%d\t%d\t%d\n",pid[i],at[i],bt[i],ct[i+1],wt[i],tat[i]);
}
printf("Average waiting time = %f\n",avgw);
printf("Average Turn Arround Time = %f\n",avgtat);
}
-----------------------------------------------------------------------------------------------------------------------------
Process ID(pid) : The Process ID is the first Thing is to be written while solving the problem. The Process ID acts like the name of the process.
Arrival Time(at) : The moment in time when a process enters the ready queue and is awaiting execution by the CPU. In other words, it is the point at which a process becomes eligible for scheduling.
Burst time(bt) : Also referred to as “execution time”. It is the amount of CPU time the process requires to complete its execution.
Completion Time(ct) : Completion time is when a process finishes execution and is no longer being processed by the CPU.
Turn Around Time(tat) : Refers to the time interval from the time of submission of a process towards the time of the finishing of the process
TAT = CT - AT
Waiting Time(wt) : Defined as the total time that is spent by the process while staying in a ready queue before it reaches the CPU.
WT = TAT - BT
Comments
Post a Comment