KEMBAR78
Os Lab6 | PDF
0% found this document useful (0 votes)
146 views9 pages

Os Lab6

The document discusses and provides code implementations for four scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF), Round Robin (RR), and Priority scheduling. For each algorithm, the code takes in process details like arrival time and burst time, performs the scheduling, and calculates metrics like waiting time and turnaround time.

Uploaded by

Lightning Gokul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views9 pages

Os Lab6

The document discusses and provides code implementations for four scheduling algorithms: First Come First Serve (FCFS), Shortest Job First (SJF), Round Robin (RR), and Priority scheduling. For each algorithm, the code takes in process details like arrival time and burst time, performs the scheduling, and calculates metrics like waiting time and turnaround time.

Uploaded by

Lightning Gokul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Operating Systems Lab – 6

Implementation of Scheduling Algorithms


Name: R Gokul
Reg.no: 18BCD7041

First come, First serve (FCFS):

#include<stdio.h>
int main(){
int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0};
int n,sum=0;
float totalTAT=0,totalWT=0;
float Avgtat=0,Avgwt=0;
printf("Enter number of processes ");
scanf("%d",&n);

printf("Enter arrival time and burst time for each process\n\n");

for(int i=0;i<n;i++)
{
printf("Arrival time of process[%d] ",i+1);
scanf("%d",&at[i]);
printf("Burst time of process[%d] ",i+1);
scanf("%d",&bt[i]);
printf("\n");
}
for(int j=0;j<n;j++)
{
sum=sum+bt[j];
ct[j]=ct[j]+sum;
}
for(int k=0;k<n;k++)
{
tat[k]=ct[k]-at[k];
totalTAT+=tat[k];
}
for(int l=0;l<n;l++)
{
wt[l]=tat[l]-bt[l];
totalWT+=wt[l];
}

Avgtat=totalTAT/n;
Avgwt=totalWT/n;
printf("Average turnaround time: %f\n",Avgtat);
printf("Average Waiting time: %f\n",Avgwt);

}
Shortest Job first (SJF):

#include<stdio.h>
int main(){
int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0};
int n,sum=0,swap,temp;
float totalTAT=0,totalWT=0;
float Avgtat=0,Avgwt=0;
printf("Enter number of processes ");
scanf("%d",&n);

printf("Enter Arrival time and Burst time for each process\n\n");

for(int i=0;i<n;i++)
{
printf("Arrival time of process[%d] ",i+1);
scanf("%d",&at[i]);
printf("Burst time of process[%d] ",i+1);
scanf("%d",&bt[i]);
printf("\n");
}
for(int c=0;c<n-1;c++){
for(int d=0;d<n-c-1;d++){
if(bt[d]<bt[d+1])
{
swap =bt[d];
bt[d]=bt[d+1];
bt[d+1]=swap;
temp=at[d];
at[d]=at[d+1];
at[d+1]=temp;
}
}

for(int j=0;j<n;j++)
{
sum=sum+bt[j];
ct[j]=ct[j]+sum;
}
for(int k=0;k<n;k++)
{
tat[k]=ct[k]-at[k];
totalTAT+=tat[k];
}
for(int l=0;l<n;l++)
{
wt[l]=tat[l]-bt[l];
totalWT+=wt[l];
}

Avgtat=totalTAT/n;
Avgwt=totalWT/n;
printf("Average turnaround time %f\n",Avgtat);
printf("Average Waiting time %f\n",Avgwt);

Round Robin (RR):

#include<stdio.h>
int main()
{
int i,j,n,time,remain,flag=0,ts;
int tot_wait=0,tot_turnaround=0,at[10],bt[10],rt[10];
printf("Enter no of Processes : ");
scanf("%d",&n);
remain=n;
for(i=0;i<n;i++)
{
printf("Enter Arrival time and Burst time for Process P %d :",i+1);
scanf("%d",&at[i]);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf("Enter time quantum");
scanf("%d",&ts);
printf("\n\nProcess\t|Turnaround time|waiting time\n\n");
for(time=0,i=0;remain!=0;)
{
if(rt[i]<=ts && rt[i]>0)
{
time+=rt[i];
rt[i]=0;
flag=1;
}
else if(rt[i]>0)
{
rt[i]=rt[i]-ts;
time+=ts;
}
if(rt[i]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
tot_wait+=time-at[i]-bt[i];
tot_turnaround+=time-at[i];
flag=0;
}
if(i==n-1)
i=0;
else if(at[i+1]<=time)
i++;
else
i=0;
}
printf("\nAvg sum_wait = %f\n",sum_wait*1.0/n);
printf("Avg sum_turnaround = %f",sum_turnaround*1.0/n);
rbturn 0;
}

Priority scheduling:

#include<stdio.h>
#include<string.h>
int main()
{
int bt[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int tot_wt=0,tot_ta=0;
float awt,ata;
int pn[10][10],t[10];
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter Process_id,Arrival time,Burst time & Priority:");

scanf("%d%d%d%d",pn[i],&at[i],&bt[i],&p[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;

temp=at[i];
at[i]=at[j];
at[j]=temp;

temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;

strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)

if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+bt[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+bt[i];
ta[i]=ft[i]-at[i];
}
tot_wt+=wt[i];
tot_ta+=ta[i];
}
awt=(float)tot_wt/n;
ata=(float)tot_ta/n;

printf("\nProcess\tArrivaltime\tExecutiontime\tPriority\tWaiti
ngtime\tTAT");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],b
t[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time:%f",awt);
printf("\nAverage turnaroundtime:%f",ata);
return 0;
}

You might also like