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

Program 6

Uploaded by

LOHITH S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program 6

Uploaded by

LOHITH S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Program-6

Develop a C program to simulate the following contiguous memory allocation


Techniques
a) Worst fit

#include<stdio.h
> int main()
{
int fragments[10], blocks[10], files[10];
int m, n, number_of_blocks, number_of_files, temp, top =
0; static int block_arr[10], file_arr[10];
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d",&number_of_blocks);
printf("Enter the Total Number of Files:\
t"); scanf("%d",&number_of_files);
printf("\nEnter the Size of the Blocks:\n");
for(m = 0; m < number_of_blocks; m++)
{
printf("Block No.[%d]:\t", m + 1);
scanf("%d", &blocks[m]);
}
printf("Enter the Size of the Files:\n");
for(m = 0; m < number_of_files; m++)
{
printf("File No.[%d]:\t", m + 1);
scanf("%d", &files[m]);
}
for(m = 0; m < number_of_files; m++)
{
for(n = 0; n < number_of_blocks; n++)
{
if(block_arr[n] != 1)
{
temp = blocks[n] -
files[m]; if(temp >= 0)
{
if(top < temp)
{
file_arr[m] =
n; top = temp;
}
}
}
fragments[m] = top;
block_arr[file_arr[m]] = 1;
top = 0;
}
}
printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\
tFragment"); for(m = 0; m < number_of_files; m++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]], fragments[m]);
}
printf("\n");
return 0;
}
Output:

b) Best Fit
#include<stdio.h>

void main()
{
int
fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999;
static int barray[20],parray[20];

printf("\n\t\t\tMemory Management Scheme - Best


Fit"); printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of
processes:"); scanf("%d",&np);

printf("\nEnter the size of the blocks:-\


n"); for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}

printf("\nEnter the size of the processes :-\


n"); for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}

for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-
p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp
;
}
}
}

fragment[i]=lowest
;
barray[parray[i]]=1
; lowest=10000;
}

printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\
tFragment"); for(i=1;i<=np && parray[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]);
}
c) First Fit
#include<stdio.h
> #define max 25
void main()
{
int
frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - First
Fit"); printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of
files:"); 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 files :-\
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) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp
)
{
ff[i]=j;
highest=temp
;
}
}
}
frag[i]=highest
; bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no:\tFile_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]);
}
Output:

You might also like