CSE3003-Operating Systems-Laboratory-Practical Exercises
CSE3003-Operating Systems-Laboratory-Practical Exercises
CSE3003-Operating Systems-Laboratory-Practical Exercises
Experiment .2 Implement CPU scheduling policies using any programming languages of your
choice.
(a) SJF
(b) Priority
(c) FCFS
(d) RoundRobin
IMPLEMENTATION DETAILS AND ASSUMPTIONS:
INPUT/s:
(i) The number of processes/jobs in the system (computed through random functions in C)
(ii) The CPU Burst (based on past history), priority (initially, compute through random function),
arrival time of process.
STEPS TO PERFORM:
(a) For SJF algorithm,
(i) We randomly generate the number of jobs. There must be a limit on the number of jobs in a
system.
(ii) The execution time of the generated jobs is also not known. Here, we are generating the CPU
burst of each job making use of the past history.
(iii) All the jobs are then arranged in a queue where searching is done to find the one with the
least CPU burst. There may be two jobs in queue with the same execution time then FCFS
approach is to be performed.
Case a) If the algorithm is non preemptive in nature, then the newly arriving job is to be added
to the job queue even though it is of lesser execution time than the one running on the processor.
Case b) Otherwise preemption is performed.
(b) For Priority scheduling,
(i) We again prefer to compute the CPU burst from past history.
(ii) Priority may be assigned on the basis of their CPU burst (simplest approach)
(iii)Priority may be assigned through some random function (within a specific range
say 1 to 100).
(iv) Priority has to be changed dynamically (to perform aging in order to avoid starvation).
Priority (preemption) and priority (non preemption) nature of priority scheduling
is performed.
(c) The FCFS scheduling is performed on the basis of arrival time irrespective of
their other parameters.
(d) Round Robin Algorithm:
I) We first have a queue where the processes are arranged in first come first serve order.
II) A quantum value is allocated to execute each process.
III) The first process is executed until the end of the quantum value. After this, an interrupt is
generated and the state is saved.
IV) The CPU then moves to the next process and the same method is followed.
V) Same steps are repeated till all the processes are over.
OUTPUT/s:
The average throughput, waiting time of process/s
---------------------------------------------------------------------------------------------------------------------
(a)SJF
Program:
#include <stdio.h>
int main()
{
int n, w[100], tot[100], i, j, awt=0, atot=0;
float avwt, avtot; struct{int p, bt;} sjf[10], temp;
printf("\n Enter The No. Of Processes:");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
printf("Enter The Burst Time For Process%d :", i);
scanf("%d", &sjf[i].bt); sjf[i].p = i; }
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(sjf[j].bt > sjf[i].bt)
{
temp = sjf[i]; sjf[i] = sjf[j]; sjf[j] = temp; }
w[1]=0; tot[1]=sjf[1].bt;
for(i=2; i<=n; i++) tot[i]=tot[i-1] + sjf[i].bt;
for(i=1; i<=n; i++)
{
w[i] = tot[i]-sjf[i].bt; awt+=w[i]; atot+=tot[i]; }
avwt=(float)awt/n; avtot=(float)atot/n;
printf("\n\nProcessID\t WaitingTime\t Turnaround Time");
for(i=1; i<=n; i++)
printf("\n\t%d\t\t%d\t\t%d", sjf[i].p, w[i], tot[i]);
printf("\n\nTotal Waiting Time:%d", awt);
printf("\n\nTotal Turnaround Time:%d", atot);
printf("\n\nAverage Waiting Time:%.2f", avwt);
printf("\n\nAverage Turnaround Time:%.2f", avtot);
return 0;
}
Output:
(b) FCFS
Program:
#include <stdio.h>
int main()
{
int n, b[10], i, t=0, w=0, r=0, a=0;
float avg, avg1;
printf ("\nEnter No. Of Processes :");
scanf("%d", &n);
printf ("\nEnter The Burst Times : \n");
for(i=1; i<=n; i++)
scanf("%d",&b[i]);
printf("\n Gantt Chart : ");
for(i=1; i<=n; i++)
printf("P%d\t",i);
printf("\n\nProcess \tBurst Time \tWaiting Time \tTurnaround
Time\n");
for(i=1; i<=n; i++)
{ t = t+w;
r = r+b[i];
printf("P%d\t\t%d\t\t%d\t\t%d\t\t\n", i, b[i], w, r);
w = w+b[i];
a = a+r;
}
avg = (float)t/n;
avg1 = (float)a/n;
printf("\n Average Waiting Time Is %f :", avg);
printf("\n Average Turnaround Time Is %f :\n", avg1);
return 0; }
Output:
(c) Priority
Program:
#include<stdio.h>
int main()
{
int
bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
total+=wt[i];
}
return 0;
}
Output:
(d) Round Robin
Program:
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d
:",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count])
;
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
Output: