Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 6

EXPERIMENT NO 8

-----------------------------------------------------------------------------------------------------------------
Name: Shubham Auti Grade:
Class:SE-COMPS-B1 Sign:
RN:07

-------------------------------------------------------------------------------------------------------------
THEORY:
In addition to the responsibility of managing processes, the operating system must efficiently
manage the primary memory of the computer. The part of the operating system which handles
this responsibility is called the memory manager.The memory manager is responsible for
allocating primary memory to processes and for assisting the programmer in loading and
storing the contents of the primary memory. Managing the sharing of primary memory and
minimizing memory access time are the basic goals of the memory manager.

The real challenge of efficiently managing memory is seen in the case of a system which has
multiple processes running at the same time. Since primary memory can be space-multiplexed,
the memory manager can allocate a portion of primary memory to each process for its own use.
However, the memory manager must keep track of which processes are running in which
memory locations, and it must also determine how to allocate and deallocate available memory
when new processes are created and when old processes complete execution. While various
different strategies are used to allocate space to processes competing for memory, three of the
most popular are Best fit, Worst fit, and First fit. Each of these strategies are described below.

First Fit

In the first fit approach is to allocate the first free partition or hole large enough which can
accommodate the process. It finishes after finding the first suitable free partition.

Advantage

Fastest algorithm because it searches as little as possible.

Disadvantage

The remaining unused memory areas left after allocation become waste if it is too smaller. Thus
request for larger memory requirement cannot be accomplished.

Best Fit
The best fit deals with allocating the smallest free partition which meets the requirement of the
requesting process. This algorithm first searches the entire list of free partitions and considers the
smallest hole that is adequate. It then tries to find a hole which is close to actual process size
needed.

Advantage

Memory utilization is much better than first fit as it searches the smallest free partition first
available.

Disadvantage

It is slower and may even tend to fill up memory with tiny useless holes.

Program
#include<stdio.h>
#define max 25
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];

int main()
{

int ch;
do
{ printf("\n1)First Fit\n2)Best Fit\n3)Exit");
printf("\nEnter your choice:-\n");
scanf("%d",&ch);
switch(ch)
{

case 1:printf("\n\tMemory Management Scheme - First Fit");


printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of Process:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the Process :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&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;
}
printf("\nProcess_no:\tProcess_size
:\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
break;
case 2:printf("\n\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of Process:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the Process :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&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)
if(lowest>temp)
{
ff[i]=j;

lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nProcess No\tProcess Size \tBlock No\tBlock
Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;i++)

printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
break;

case 3:exit(0); break;

default:break;
}
}while(ch!=3);
}
Output
Conclusion : Thus we have successfully studied and implemented dynamic partitioning
placement algorithms i.e Best Fit and First Fit.

You might also like