Assignment No:- 04
Name:- Shreyash Shahane Roll No:- 222059
Gr No:- 21911206
Batch:- B-3
•Aim:-
Program to show the demonstration of
Scheduling Algorithms: 1)FCFS 2)SJF
1)FCFS:-
• Theory:-
First Come First Served (FCFS) is a
Non-Preemptive scheduling algorithm. FIFO (First In First
Out) strategy assigns priority to process in the order in
which they request the processor. The process that
requests the CPU first is allocated the CPU first. This is
easily implemented with a FIFO queue for managing the
tasks.
As the process come in, they are put at
the end of the queue. As the CPU finishes each task, it
removes it from the start of the queue and heads on to
the next task.
• Code FCFS:-
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum
20):");
scanf("%d",&n);
printf("\nEnter Process Burst Time\n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
printf("\nProcess\t\tBurst Time\tWaiting
Time\tTurnaround Time");
//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
return 0;
}
• Output:-
2)SJF:-
• Theory:-
Here you will get C program for (SJF)
scheduling algorithm.
In shortest job first scheduling
algorithm, the processor selects the waiting process with
the smallest execution time to execute next.
• 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);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process
number
}
//sorting burst time in ascending order using
selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
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;
}
wt[0]=0; //waiting time for first
process will be zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting
time
total=0;
printf("\nProcess\t Burst Time \tWaiting
Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround
time
total+=tat[i];
printf("\np%d\t\t %d\t\t
%d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n; //average turnaround
time
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
• Output:-