0% found this document useful (0 votes)
15 views17 pages

Os Practical File

os practical file

Uploaded by

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

Os Practical File

os practical file

Uploaded by

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

IES University, Bhopal

Name of student : SAMRIDDHI JAIN

Enrollment Number : 2IES21CAI001

Subject : Operating System

Subject code : AL 521

Branch : CSE- AIML

Semester : 5th

Session : July 2023 – Dec 2023


Department of Computer Science and Engineering

CONTENTS

S.No. NAME OF THE PROGRAM Date Remark


1.
WAP to implement First Come First
Serve (FCFS) Scheduling
2.
WAP to implement shortest job
first (SJF) scheduling
3.
WAP to implement Priority based
scheduling
4.
WAP to implement Round Robin
5.
WAP to implement LRU page
replacement algorithm
6.
WAP to implement Optimal page
replacement algorithm
PROGRAM 1

Write a program to implement First Come First Serve (FCFS)


Scheduling.

#include<stdio.h>
#include<conio.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

Enter the total no. of processes:3


Enter the process burst time
P[1]: 24
P[2] : 3
P[3] : 3

Process Burst time Waiting time turnaround time


P[1 ] 24 0 24
P[2] 3 24 27
p[3 ] 3 27 30

Avg. Waiting time= 17.000000


Avg. Turnaround time=27.000000
PROGRAM 2

Write a program to implement shortest job first (SJF)


scheduling.

#include<stdio.h>
#include<conio.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

Enter the total no. of processes:4


Enter the process burst time
P1: 4
P2 : 8
P3 : 3
P4 : 7
Process Burst time Waiting time turnaround time
P3 3 0 3
P1 4 3 7
P4 7 7 14
P2 8 14 22

Avg. Waiting time= 6.000000


Avg. Turnaround time=11.50000
PROGRAM 3
Write a program to implement Priority based scheduling.

#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_t
at;
printf("Enter Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n");


for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}

//sorting burst time, priority and process number in ascending


order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}

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;
}

wt[0]=0; //waiting time for first process is 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=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=total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}

Output
Enter the total no. of processes:4
Enter the process burst time and priority
P1
Burst time :6
priority: 3
P2
Burst time :2
priority: 2
P3
Burst time :14
priority: 1

P4
Burst time :6
priority: 4

Process Burst time Waiting time turnaround time


P3 14 0 14
P2 2 14 16
P1 6 16 22
P4 6 22 28

Avg. Waiting time= 13


Avg. Turnaround time=20

PROGRAM 4
Write a program to implement Round Robin Scheduling
#include&lt;conio.h&gt;
#include&lt;stdio.h&gt;
void main()
{
intprcs[10],n,i,arv[10],brst[10],temp,tempb,tempp,sumb,j;
inttempa,t,time,wt[10],wtt[10],max,maxi,y=1;
floatst=0.0,tt=0.0,wat=0.0;
clrscr();
printf(&quot;enter the number of processes&quot;);
scanf(&quot;%d&quot;,&amp;n);
for(i=0;i&lt;n;i++)
{prcs[i]=i;
printf(&quot;\nenter the arrival time for P%d&quot;,i);
scanf(&quot;%d&quot;,&amp;arv[i]);
printf(&quot;enter the burst time for P%d&quot;,i);
scanf(&quot;%d&quot;,&amp;brst[i]);
wt[i]=0;
wtt[i]=0;}
printf(&quot;enter the time slice&quot;);
scanf(&quot;%d&quot;,&amp;time);
printf(&quot;process\t arrival time \t burst time\n&quot;);
for(i=0;i&lt;n;i++)
{printf(&quot;P%d\t %d \t\t %d\n&quot;,prcs[i],arv[i],brst[i]);}
printf(&quot;\n&quot;);
for(i=0;i&lt;n;i++)
{for(j=0;j&lt;n-1;j++)
{if(arv[j]&gt;arv[j+1])
{tempa=arv[j];
arv[j]=arv[j+1];
arv[j+1]=tempa;
tempb=brst[j];
brst[j]=brst[j+1];
brst[j+1]=tempb;
tempp=prcs[j];
prcs[j]=prcs[j+1];
prcs[j+1]=tempp; }}}
max=brst[0];
maxi=0;
for(i=1;i&lt;n;i++)
{if(max&lt;brst[i])
{max=brst[i];
maxi=i;} }
while( y&gt;0 )
{ for(i=0;i&lt;n;i++)
{if (brst[i]&gt;0)
{if (brst[i]&gt;time)
{brst[i]=brst[i]-time;
st=time+st;
wt[i]=wt[i]+time;}
else
{ wtt[i]=st-wt[i];
st=st+brst[i];
printf(&quot;wtt of %d is %d&quot;,i,wtt[i]);
wat=wat+wtt[i]-arv[i];
tt=tt+st-arv[i];
printf(&quot;\nfor %d waiting time is %f \nturn around time is
%f&quot;,i,wat/n,tt/n);
brst[i]=0;} }
if(brst[maxi]==0)
y=0;}}
printf(&quot;\nwaiting time is %f \nturn around time is
%f&quot;,wat/n,tt/n);
getch();}

OUTPUT
Enter the number of processes 3
Enter the arrival time for P0 0
Enter the burst time for P0 24
Enter the arrival time for P1 0
Enter the burst time for P1 3
Enter the arrival time for P2 0
Enter the burst time for P2 3
Enter the time slice 4
process arrival time burst time
P0 0 24
P1 0 3
P2 0 3
waiting time is 5.666667
turn around time is 15.666667

PROGRAM 5
Write a program to implement LRU page replacement algorithm

#include<stdio.h>
#include<conio.h>
intfr[20],frsize;
void main()
{
void display();
int p[50],i,j,fs[20],n;
int index,k,l,flag1=0,flag2=0;
floatpfr,pf=0;
clrscr();
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&frsize);
for(i=0;i<frsize;i++)
fr[i]=-1;
for(j=0;j<n;j++)
{
flag1=0,flag2=0;
for(i=0;i<frsize;i++)
if(fr[i]==p[j])
{
flag1=1;
flag2=1;
break;
}
if(flag1==0) //if there is an empty place
{
for(i=0;i<frsize;i++)
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
pf++;
break;
}
}
if(flag2==0) //if there is no empty place
{
for(i=0;i<frsize;i++)
fs[i]=0;
for(k=j-1,l=1;l<=frsize-1;l++,k--)
{
for(i=0;i<frsize;i++)
{
if(fr[i]==p[k])
fs[i]=1;
}
}
for(i=0;i<frsize;i++)
{
if(fs[i]==0)
index=i;
}
fr[index]=p[j];
pf++;
}
display();
}
pfr=pf/frsize;
printf("\n no of page faults :%f",pf);
printf("\n page fault rate : %f", pfr);
getch();
}
void display()
{
inti;
printf("\n");
for(i=0;i<frsize;i++)
printf("\t%d",fr[i]);
}

Output

ENTER THE NUMBER OF FRAMES : 3


7 -1 -1
7 0 -1
7 0 1
2 0 1
2 0 1
2 0 3
2 0 3
4 0 3
4 0 2
4 3 2
0 3 2
0 3 2
0 3 2
1 3 2
1 3 2
1 0 2
1 0 2
1 0 7
1 0 7
1 0 7
no of page faults :12.000000
page fault rate : 4.000000

PROGRAM 6
Write a program to implement Optimal page replacement
algorithm

#include<stdio.h>
#include<conio.h>
int a[21],f[8],n,k;
void main()
{
intft,c,i,j,p,m,min;
clrscr();
printf("enter the no. of frames\t");
scanf("%d",&k);
printf("enter the length of string\t");
scanf("%d",&n);
printf("enter the %d string enteries\t",n);
for(i=1;i<=n;i++)
{scanf("%d",&a[i]);}

for(i=1;i<=k;i++)
{f[i]=-1;}
p=0;
ft=1;
for(i=1;i<=n;i++)
{c=0;
for(j=1;j<=k;j++)
{if(a[i]==f[j])
{c=1;}}
if (c==1)
{p=p;}

else
{if(f[i]==-1)
{f[ft]=a[i];
ft++;}
else
{min=selminp(i);
f[min]=a[i];}
p++;
}
printf("\nelements in frame after %d iteration:\t",i);

for(m=1;m<=k;m++)
{if (f[m]==-1)
printf("NIL\t");
else
printf("%d\t",f[m]);}
}
printf("\npage faults are %d",p);
printf("\npage fault rate is %f",p/2.0);
getch();
}
intselminp(intcurposit)
{inti,m,futp[10],ch,t,c;
for(i=1;i<=k;i++)
for(m=1;m<=k;m++)
{c=0;
for(i=curposit+1;(i<=n)&&(c==0);i++)
{if (f[m]==a[i])
{futp[m]=n-i+1;
c=1;}
else
{futp[m]=-1;}
}
}
t=futp[1];
ch=1;

for(i=2;i<=k;i++)
{if(t>futp[i])
{t=futp[i];
ch=i;}}
returnch;
}

Output
Enter the no. of frames 3
Enter the length of string 15
Enter the 15 string enteries 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2

Elements in frame after 1 iteration: 7 NIL NIL


Elements in frame after 2 iteration: 7 0 NIL
Elements in frame after 3 iteration: 7 0 1
Elements in frame after 4 iteration: 2 0 1
Elements in frame after 5 iteration: 2 0 1
Elements in frame after 6 iteration: 2 0 3
Elements in frame after 7 iteration: 2 0 3
Elements in frame after 8 iteration: 2 4 3
Elements in frame after 9 iteration: 2 4 3
Elements in frame after 10 iteration: 2 4 3
Elements in frame after 11 iteration: 2 0 3
Elements in frame after 12 iteration: 2 0 3
Elements in frame after 13 iteration: 2 0 3
Elements in frame after 14 iteration: 2 1 3
Elements in frame after 15 iteration: 2 1 3
Page faults are 8
Page fault rate is 2.666667

You might also like