0% found this document useful (0 votes)
273 views

Operating System Lab Assignment

The document contains code for implementing various memory allocation algorithms and page replacement algorithms: 1. It provides C++ code to simulate first fit, best fit, and worst fit memory allocation algorithms. It calculates internal and external fragmentation for each approach. 2. It also provides C++ code to simulate FIFO, LRU and OPT page replacement algorithms. FIFO replaces the oldest page, LRU replaces the least recently used page, and OPT replaces the page that will be used furthest in the future. 3. The document appears to be notes for an operating systems lab assignment, written by a student with the initials N.R.R. and student ID 18BIT0126. It focuses
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)
273 views

Operating System Lab Assignment

The document contains code for implementing various memory allocation algorithms and page replacement algorithms: 1. It provides C++ code to simulate first fit, best fit, and worst fit memory allocation algorithms. It calculates internal and external fragmentation for each approach. 2. It also provides C++ code to simulate FIFO, LRU and OPT page replacement algorithms. FIFO replaces the oldest page, LRU replaces the least recently used page, and OPT replaces the page that will be used furthest in the future. 3. The document appears to be notes for an operating systems lab assignment, written by a student with the initials N.R.R. and student ID 18BIT0126. It focuses
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/ 16

OPERATING SYSTEMS

LAB ASSIGNMENT-04
18BIT0126
N.ROHITH REDDY

1.)Consider a memory hole of size 1kb initially. When a sequence of


memory request arrives as following, illustrate the memory allocation
by various approaches and calculate the total amount memory wasted
by external fragmentation and internal fragmentation in each
approach.
a. First fit; b. Best fit c. Worst fit (Easy)

a.) First fit


#include<iostream>

using namespace std;

int main()

{ cout<<"Program to implement first fit file allocation scheme ";

int i,j,k,np,nf,f;

cout<<"Enter the number of partition: "; cin>>np;

int sp[np]; int flag[np];

cout<<"Enter the size of each partition: ";

for(i=0;i<np;i++)

{
cin>>sp[i];flag[i]=0;

cout<<"Enter the number of files: "; cin>>nf;

int sf[nf];

cout<<"Enter the size of each file:";

for(i=0;i<nf;i++) cin>>sf[i];

cout<<"implementing first fit stratergy\n";

cout<<"File No.|| File Size || Partition number ||partition size\n";

for(i=0;i<nf;i++) {f=0;// for(j=0;j<np;j++)

//cout<<"asnjklasnkld"; if((flag[j]==0)&&(f==0))

//cout<<"u qwehqwijrijq;wm;orkqerl;p'[werq ";

if(sf[i]<sp[j])

flag[j]=1;

cout<<i<<"||"<<sf[i]<<"||"<<j<<"||"<<sp[j]<<endl;

f=1;

}}

} if

(f==0)

cout<<"No partitionis bigenoughtosupportthe file"<<i;

}
b.) Best fit
#include<iostream>

using namespace std;

int main()

{ cout<<"Program implementing bestfit strategy";

cout<<endl;

int i,j,k,np,nf,f,b;

cout<<"Enter the number of paartition :";

cin>>np; int sp[np],flag[np];

cout<<"Enter the size of each partition";

for(i=0;i<np;i++)

cin>>sp[i];flag[i]=0;

}
cout<<"enter the number of files";

cin>>nf; int sf[nf];

for(i=0;i<nf;i++)

{ cin>>sf[i];

cout<<"Implementing Best Fit Algorithm strategy";

cout<<"File no.||File Size|| Partition number || partition Size \n";

for(i=0;i<nf;i++)

{ f=-1; for(j=0;j<np;j++)

if(flag[j]==0)

if(sf[i]<=sp[j])

if(f==-1)

{f=j;flag[j]=1;}

else

if(sp[j]<sp[f])

{flag[f]=0;f=j;flag[j]=1;}

} if(f==-1)

cout<<"No Partition is big enough to support the file !!"; else

cout<<i<<"\t"<<sf[i]<<"\t"<<f<<"\t"<<sp[f]<<endl;

} return 1;

}
c.)Worst fit (Easy)
#include<iostream>

using namespace std;

int main() {

cout<<"Program implementing worst fit strategy";

cout<<endl;

int i,j,k,np,nf,f,b; cout<<"Enter thenumber of partition :";

cin>>np;

int sp[np],flag[np];

cout<<"Enter the size of each partition";

for(i=0;i<np;i++)

cin>>sp[i];flag[i]=0;

cout<<"enter the number of files";

cin>>nf;
cout<<"Enter the size of each file : ";

int sf[nf]; for(i=0;i<nf;i++)

{ cin>>sf[i];

cout<<"Implementing Worst Fit Algorithm strategy"<<endl;

cout<<"File no.||File Size|| Partition number || partition Size \n";

for(i=0;i<nf;i++) { f=-1; for(j=0;j<np;j++)

if(flag[j]==0)

if(sf[i]<=sp[j])

if(f==-1)

{f=j;flag[j]=1;}

else

if(sp[j]>sp[f])

{flag[f]=0;f=j;flag[j]=1;}

} if(f==-1)

cout<<"No Partition is big enough to support the file !!";

else cout<<i<<"\t"<<sf[i]<<"\t"<<f<<"\t"<<sp[f]<<endl;

return 1;}
2.) Write a program to implement the page replacement algorithms.
a. FIFO b. LRU c. OPT (Medium)

a. )FIFO

#include<iostream>

using namespace std;

int main() {

int n,m,i,j,k,a,flag=0,pos,hits=0,flag1=0,pos1=0; int

fr[30][10],p[30];

cout<<"Enter the no of pages : ";

cin>>n;
cout<<"\nEnter the no of frames in memory : ";

cin>>m;

cout<<"\nEnter the page names :\n";

for(i=0;i<n;i++)

{cin>>p[i];}

for(i=0;i<n;i++)

{for(j=0;j<m;j++)

{ fr[i][j]=0; }}

for(i=0;i<n;i++)

{flag=0;flag1=0;

if(i>0)

{a=i;

for(k=0;k<m;k++)

{fr[i][k]=fr[a-1][k];}}

for(j=0;j<m;j++)

{if(p[i]==fr[i][j])

{flag=1;

hits++;}}

if(flag==0)

{for(k=0;k<m;k++)

{if(fr[i][k]==0)

{pos=k; flag1=1;

break;}}

if(flag1==1)

{fr[i][pos]=p[i];

pos1=pos+1;

if(pos1>=m)

pos1=0;

}
else

{ fr[i][pos1]=p[i];

pos1=pos1+1;

if(pos1>=m)

pos1=0;

}}

else continue;}

//printing

for(i=0;i<n;i++)

{for(j=0;j<m;j++)

{cout<<fr[i][j];}

cout<<"\n";}

cout<<"\n\nThe no of page faults is: "<<n-hits<<endl;

return 0;}
b. )LRU

#include<stdio.h>

int findLRU(int time[], int n){ int

i, minimum = time[0], pos = 0;

for(i = 1; i < n; ++i){

if(time[i] < minimum)

{ minimum= time[i]; pos= i;}}

return pos;}

int main() {

int no_of_frames, no_of_pages, frames[10], pages[30], counter

= 0, time[10], flag1, flag2, i, j,

pos, faults = 0;
printf("Enter number of frames: ");

scanf("%d", &no_of_frames);

printf("Enter number of pages: ");

scanf("%d", &no_of_pages);

printf("Enter reference string: ");

for(i = 0; i < no_of_pages; ++i){

scanf("%d", &pages[i]);

for(i = 0; i < no_of_frames; ++i){

frames[i] = -1;

for(i = 0; i < no_of_pages; ++i){

flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j){

if(frames[j] == pages[i]){ counter++;

time[j] = counter; flag1 = flag2 = 1;

break;

}}

if(flag1 == 0){ for(j = 0; j <no_of_frames; ++j)

{ if(frames[j] ==-1)

{ counter++; faults++;frames[j] = pages[i]; time[j] =counter; flag2 = 1; break;

}}}

if(flag2 == 0){ pos =

findLRU(time, no_of_frames);

counter++; faults++;

frames[pos] = pages[i]; time[pos] =

counter;}

printf("\n");

for(j = 0; j < no_of_frames; ++j){


printf("%d\t", frames[j]);}}

printf("\n\nTotal Page Faults = %d", faults);

return 0;

c. )OPT
#include<iostream>

using namespace std;

int main() {

int

n,m,i,j,k,l=0,a,len,count=0,flag=0,pos=0,hits=0,flag1=0,pos1=0,pos2=0,max;

int fr[30][10],p[30],temp[4]; cout<<"Enter the no of pages : ";

cin>>n;
cout<<"Enter the no of frames in a page : ";

cin>>m;

cout<<"\nEnter the pagenumbers :\n ";

for(i=0;i<n;i++)

{cin>>p[i];}

for(i=0;i<n;i++)

{ for(j=0;j<m;j++)

{ fr[i][j]=0; }}

for(i=0;i<n;i++)

{flag=0;flag1=0;

if(i>0) { a=i;

for(k=0;k<m;k++)

{fr[i][k]=fr[a][k];}}

for(j=0;j<m;j++)

{if(p[i]==fr[i][j])

{ flag=1; hits++; }}

if(flag==0)

{for(k=0;k<m;k++)

{if(fr[i][k]==0)

{ pos=k;flag1=1;break; } }

if(flag1==1)

{ fr[i][pos]=p[i];}

else

{for(j=0;j<m;j++)

{ len=i+1;

while(fr[i][j]!=p[len]) {

len++;count++; }

temp[l++]=count+1; count=0;}

max=temp[0];
for(j=0;j<m;j++)

{if(temp[j]>max) { max=temp[j]; pos2=j; }}

fr[i][pos2]=p[i];

}} else

continue;}

for(i=0;i<n;i++) {

for(j=0;j<m;j++)

{cout<<fr[i][j];}

cout<<"\n";}

cout<<"\n\nThe no of page faults is: "<<n-hits;

return 0;

You might also like