Problem
No: 09
Name
of the problem: Memory management scheme-Worst fit
Theory: It’s
a method to map segments to
holes (spaces) in virtual memory.
It selects the largest available hole in memory that can fit a needed segment,
so as to leave a large hole for other segments.
Code:
#include<bits/stdc++.h>
using
namespace std;
#define max 25
int main()
{
int
i,j,b[max],f[max],frag[max],highest,nb,nf,temp;
static int bf[max],ff[max];
cout<<"Memory management scheme
- worst fit";
cout<<"enter the number of
blocks :";
cin>>nb;
cout<<"enter the number of files
:";
cin>>nf;
cout<<endl<<"enter the
size of the blocks :"<<endl;
for(i=1;i<nb+1;i++)
{
cout<<"block
"<<i<<":";
cin>>b[i];
}
cout<<endl<<"enter the
size of the files :"<<endl;
for(i=1;i<nf+1;i++)
{
cout<<"file
"<<i<<":";
cin>>f[i];
}
cout<<"file no:\tfile size\tblock
no\tblock size\tfragment"<<endl;
for(i=1;i<=nf;i++)
{highest=0;
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=highest)
{
ff[i]=j;
highest=temp;
}}}
frag[i]=highest;
bf[ff[i]]=1;
cout<<i<<"\t\t"<<f[i]<<"\t\t"<<ff[i]<<"\t\t"<<b[ff[i]]<<"\t\t"<<frag[i]<<endl;
}}
Problem
No: 10
Name
of the problem: Memory
management scheme-Best Fit
Theory:
In
the first fit, the partition is allocated which is first sufficient from the
top of Main Memory.
Code:
#include<bits/stdc++.h>
using namespace std;
#define max 25
int main()
{
int
i,j,b[max],f[max],frag[max],lowest,nb,nf,temp;
static int bf[max],ff[max];
cout<<"Memory management scheme
- Best fit";
cout<<"enter the number of
blocks :";
cin>>nb;
cout<<"enter the number of files
:";
cin>>nf;
cout<<endl<<"enter the
size of the blocks :"<<endl;
for(i=1;i<nb+1;i++)
{
cout<<"block
"<<i<<":";
cin>>b[i];
}
cout<<endl<<"enter the
size of the files :"<<endl;
for(i=1;i<nf+1;i++)
{cout<<"file
"<<i<<":";
cin>>f[i];
}
cout<<"file no:\tfile size\tblock
no\tblock size\tfragment"<<endl;
for(i=1;i<=nf;i++)
{lowest=1000;
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
if(temp<lowest)
{
ff[i]=j;
lowest=temp;
} } } }
frag[i]=lowest;
bf[ff[i]]=1;
cout<<i<<"\t\t"<<f[i]<<"\t\t"<<ff[i]<<"\t\t"<<b[ff[i]]<<"\t\t"<<frag[i]<<endl;
}}
Problem
No: 11
Name
of the problem: Memory management scheme-First Fit
Theory:
There may be many holes in the memory, so the operating system, to reduce the amount of time it spends analyzing the available spaces,
begins at the start of primary memory and allocates memory from the first hole
it encounters large enough
to satisfy the request.
Code:
#include<bits/stdc++.h>
using
namespace std;
#define max 25
int main()
{
int i,j,b[max],f[max],frag[max],nb,nf,temp;
static int bf[max],ff[max];
cout<<"Memory management scheme
- First fit \n";
cout<<"enter the number of
blocks: ";
cin>>nb;
cout<<"enter the number of
files";
cin>>nf;
cout<<"enter the size of the
blocks :"<<endl;
for(i=1;i<nb+1;i++)
{
cout<<"block
"<<i<<":";
cin>>b[i];
}
cout<<"enter the size of the
files :"<<endl;
for(i=1;i<nf+1;i++)
{
cout<<"file
"<<i<<":";
cin>>f[i];
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}}}
frag[i]=temp;
bf[ff[i]]=1;
}
cout<<"file no:\tfile
size\tblock no\tblock size\tfragment"<<endl;
for(i=1;i<=nf;i++)
{cout<<i<<"\t\t"<<f[i]<<"\t\t"<<ff[i]<<"\t\t"<<b[ff[i]]<<"\t\t"<<frag[i]<<endl;
}}
No comments:
Post a Comment