0% found this document useful (0 votes)
4 views5 pages

Oslab 2

The document contains C code implementations for three memory allocation strategies: Best Fit (BFT), First Fit (FFT), and Worst Fit (WFT). Each implementation prompts the user to input the number of memory blocks and files, and then allocates files to blocks based on the chosen strategy, displaying the results including any fragmentation. The code is structured to handle user input and output the allocation results in a formatted manner.
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)
4 views5 pages

Oslab 2

The document contains C code implementations for three memory allocation strategies: Best Fit (BFT), First Fit (FFT), and Worst Fit (WFT). Each implementation prompts the user to input the number of memory blocks and files, and then allocates files to blocks based on the chosen strategy, displaying the results including any fragmentation. The code is structured to handle user input and output the allocation results in a formatted manner.
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/ 5

#include<stdio.

h>
#define max 20

int main()
{
int bsize[max], fsize[max], nb, nf, temp, low = 10000;
static int bflag[max], fflag[max];
int i, j;

printf("\nEnter the number of blocks: ");


scanf("%d", &nb);

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


{
printf("Enter the size of memory block %d: ", i + 1);
scanf("%d", &bsize[i]);
}

printf("\nEnter the number of files: ");


scanf("%d", &nf);

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


{
printf("\nEnter the size of file %d: ", i + 1);
scanf("%d", &fsize[i]);
}
for (i = 0; i < nf; i++)
{
for (j = 0; j < nb; j++)
{
if (bflag[j] != 1)
{
temp = bsize[j] - fsize[i]; BFT
if (temp >= 0)
{
if (low > temp)
{
fflag[i] = j;
low = temp;
}
}
}
}
if (fflag[i] != -1) {
bflag[fflag[i]] = 1;
}
low = 10000;
}

// Display the result


printf("\nFile no\tFile size\tBlock no\tBlock size");
for (i = 0; i < nf; i++)
{
if (fflag[i] != -1) {
printf("\n%d\t%d\t\t%d\t\t%d", i + 1, fsize[i], fflag[i] +
1, bsize[fflag[i]]);
}
}
return 0;
}

BFT

FFT

#include<stdio.h>
#define max 25
void main()
{
int frag[max], b[max], f[max], i, j, nb, nf, temp;
static int bf[max], ff[max];

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("\nEnter 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) {
temp = b[j] - f[i];
if (temp >= 0) {
ff[i] = j;
break;
}
}
}
frag[i] = temp;
bf[ff[i]] = 1;
}

printf("\nFile_no\tFile_size\tBlock_no\tBlock_size\tFragment\n");
for (i = 1; i <= nf; i++) {
printf("\n%d\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]],
frag[i]);
}
}

FFT
WFT

#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 Worst 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 = 0; i < nb; i++)
{
printf("Block %d: ", i + 1);
scanf("%d", &b[i]);
}
printf("Enter the size of the files:\n");
for(i = 0; i < nf; i++)
{
printf("File %d: ", i + 1);
scanf("%d", &f[i]);
}
for(i = 0; i < nb; i++)
{
bf[i] = 0;
}
for(i = 0; i < nf; i++)
{
highest = -1;
for(j = 0; j < nb; j++)
{
if(bf[j] == 0)
{
temp = b[j] - f[i];

if(temp >= 0 && (highest < temp))


{
ff[i] = j;
highest = temp;
}
}
}
if(highest != -1)
{
frag[i] = highest;
bf[ff[i]] = 1;
}
else
{
frag[i] = -1;
}
}
printf("\nFile_no\tFile_size\tBlock_no\tBlock_size\tFragment\n");
for(i = 0; i < nf; i++)
{
if(frag[i] != -1)
{
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, f[i], ff[i] +
1, b[ff[i]], frag[i]);
}
else
{
printf("%d\t\t%d\t\tNot Allocated\t\t-\t\t-\n", i + 1,
f[i]);
}
}
}

WFT

You might also like