Professional Documents
Culture Documents
PROGRAM - 7
AIM
Write a program to implement First Come First Scheduling Algorithm.
DESCRIPTION
First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest
scheduling algorithm. FIFO simply queues processes in the order that they arrive in the
ready queue.
In this, the process that comes first will be executed first and next process starts only after
the previous gets fully executed.
Here we are considering that arrival time for all processes is 0.
ALGORTIHM
Step 1: Input the processes along with the burst time.
Step 2: Find waiting time(wt) for all processes.
Step 3: As first process that comes need not to wait so waiting time for process 1 will be 0
i.e. wt[0] = 0.
Step 4: Find waiting time for all the processes i.e. for the process
wt[i] = b[i-1] + wt[i-1]
Step 5: Find turn around time = waiting time + burst time for all the processes.
Step 6: Find average waiting time and average turn around time.
Step 5: END
CODE
#include<stdio.h>
int main()
{
int n,a[10],b[10],t[10],w[10],g[10],i,m;
float att=0,awt=0;
for(i=0;i<10;i++)
{
a[i]=0; b[i]=0; w[i]=0; g[i]=0;
}
printf("Enter the number of process: ");
scanf("%d",&n);
printf("Enter the burst times: ");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
printf("\nEnter the arrival times: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
g[0]=0;
for(i=0;i<10;i++)
g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
{
w[i]=g[i]-a[i];
t[i]=g[i+1]-a[i];
awt=awt+w[i];
18 | R i s h a b h C h a n d r a | 2 K 1 7 / C O / 2 6 4
att=att+t[i];
}
awt =awt/n;
att=att/n;
printf("\n\tProcess\tWaiting Time\tTurn Around Time\n");
for(i=0;i<n;i++)
{
printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]);
}
printf("The average waiting time is %f\n",awt);
printf("The average turn around time is %f\n",att);
return 0;
}
OUTPUT
DISCUSSIONS
Non-preemptive
Average Waiting Time is not optimal
Cannot utilize resources in parallel: Results in Convoy effect (Consider a situation
when many IO bound processes are there and one CPU bound process. The IO bound
processes have to wait for CPU bound process when CPU bound process acquires
CPU. The IO bound process could have better taken CPU for some time, then used IO
devices).
19 | R i s h a b h C h a n d r a | 2 K 1 7 / C O / 2 6 4
PROGRAM - 8
AIM
Write a program to implement Shortest Job first Scheduling.
DESCRIPTION
Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting
process with the smallest execution time to execute next. SJN is a non-preemptive
algorithm.
Shortest Job first has the advantage of having minimum average waiting time among
all scheduling algorithms.
It is a Greedy Algorithm.
It may cause starvation if shorter processes keep coming. This problem can be solved
using the concept of aging.
It is practically infeasible as Operating System may not know burst time and
therefore may not sort them. While it is not possible to predict execution time,
several methods can be used to estimate the execution time for a job, such as a
weighted average of previous execution times. SJF can be used in specialized
environments where accurate estimates of running time are available.
ALGORTIHM
Step 1: START
Step 2: Sort all the processes in increasing order according to burst time.
Step 3: Then Simply apply FCFS
Step 5: END
CODE
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
total+=wt[i];
}
OUTPUT
DISCUSSIONS
Shortest Processes are handled very easily.
It can lead to starvation of long processes.
Sorting takes place according to burst time.
SJF has very low average waiting time.