Process States
Process States
• It is a non-preemptive algorithm.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
• The average waiting time is quite long.
For eg., The processes. P1. P2. P3 with burst time:
24, 3 and 3 respectively are given.
In a Gantt chart,
Drawbacks:
i) Long waiting time.
ii) Convoy effect – All the processes waiting for one big process to get off
the CPU. This results in low CPU utilisation and low device utilization.
Write C program to simulate the following CPU scheduling algorithms: FCFS
#include<stdio.h> tt[i+1]=tt[i]+bt[i+1];
void main() w1=w1+wt[i];
{ t1=t1+tt[i];
int i,j,bt[10],n,wt[10],tt[10]; }
float w1=0,t1=0,awt,att; awt=w1/n; //calculating Avg. Waiting Time
printf("Enter the no. of Processes:\n"); att=t1/n; //calculating Avg. Turn-around Time
scanf("%d",&n); printf("\nBT\t WT\t TT\n");
printf("Enter the CPU Burst-time of Processes: :\n "); for(i=0;i<n;i++)
for(i=0;i<n;i++) printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
scanf("%d",&bt[i]); printf(“Avg. Waiting time=%f\n Avg. Turn-around time=%f\n",awt,att);
for(i=0;i<n;i++)
}
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
OUTPUT:
Enter the no. of Processes:
3
Enter the CPU Burst-time of Processes:
10
8
12
BT WT TT
10 0 10
8 10 18
12 18 30
Avg. Waiting time =9.3333333
Avg. Turn-around time =19. 3333334
2. Shortest-Job-First (SJF) Scheduling: When the CPU is available,
it is allocated to the process with the smallest CPU burst time.
SJF can be either non-preemptive or pre-emptive(SRTF). SJF is optimal,
i.e., gives min’ average waiting time for a given set of Processes.
#include<stdio.h>
void main() for(i=0;i<n;i++)
{ { wt[0]=0;
int i,j,bt[10],t,n,wt[10],tt[10],w1=0,t1=0; tt[0]=bt[0];
float awt,at; wt[i+1]=bt[i]+wt[i];
printf("Enter no. of processes:\n"); tt[i+1]=tt[i]+bt[i+1];
scanf("%d",&n); w1=w1+wt[i];
printf("Enter the burst time of processes:\n"); t1=t1+tt[i];
for(i=0;i<n;i++) }
scanf("%d",&bt[i]); awt=w1/n;
for(i=0;i<n;i++) at=t1/n;
{ printf("\nbt\t wt\t tt\n");
for(j=i;j<n;j++) for(i=0;i<n;i++)
if(bt[i]>bt[j]) //Comparing the burst-time of processes and choosing the process printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
with shortest burst time.
printf("awt=%f\nat=%f\n",awt,aTt);
{
}
t=bt[i]; bt[i]=bt[j]; bt[j]=t;
}
}
printf("Order of Execution(SJFS):");
for(i=0;i<n;i++)
printf("%d,",bt[i]);
OUTPUT:
BT WT TT
10 0 10
4 10 14
6 14 20