0% found this document useful (0 votes)
7 views6 pages

Dynamic Memory Allocation Algorithm

The document outlines three dynamic memory allocation algorithms: First-Fit, Best-Fit, and Worst-Fit, each implemented in C. Each algorithm allocates memory blocks to files based on different strategies to minimize fragmentation. The document includes code snippets for each algorithm along with prompts for user input and output formatting.

Uploaded by

bgmimaste69
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)
7 views6 pages

Dynamic Memory Allocation Algorithm

The document outlines three dynamic memory allocation algorithms: First-Fit, Best-Fit, and Worst-Fit, each implemented in C. Each algorithm allocates memory blocks to files based on different strategies to minimize fragmentation. The document includes code snippets for each algorithm along with prompts for user input and output formatting.

Uploaded by

bgmimaste69
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

Dynamic Memory Allocation Algorithm

23BAI1229

Shobhit Singh

1. First-Fit
#include <stdio.h>
#define max 25

int 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]);
bf[i] = 0;
}

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 < nf; i++) {


highest = -1;
for(j = 0; j < nb; j++) {
if(bf[j] == 0) {
temp = b[j] - f[i];
if(temp >= 0 && temp > highest) {
ff[i] = j;
highest = temp;
}
}
}
if (highest != -1) {
frag[i] = highest;
bf[ff[i]] = 1;
} else {
ff[i] = -1;
}
}

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

return 0;
}

Output:
2. Best-fit
#include <stdio.h>
#define max 25

int main() {
int frag[max], b[max], f[max], i, j, nb, nf, temp, lowest = 10000;
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 = 0; i < nb; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &b[i]);
bf[i] = 0;
}

printf("\nEnter 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 < nf; i++) {


lowest = 10000;
for(j = 0; j < nb; j++) {
if(bf[j] == 0) {
temp = b[j] - f[i];
if(temp >= 0 && temp < lowest) {
ff[i] = j;
lowest = temp;
}
}
}
frag[i] = lowest;
bf[ff[i]] = 1;
}

printf("\nFile No\tFile Size\tBlock No\tBlock Size\tFragment\n");


for(i = 0; i < nf; i++) {
if(ff[i] != -1) {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i + 1, f[i], ff[i] + 1,
b[ff[i]], frag[i]);
} else {
printf("\n%d\t\t%d\t\tNot Allocated", i + 1, f[i]);
}
}

return 0;
}

Output :
3. Worst-fit
#include <stdio.h>
#define max 25

int main() {
int frag[max], b[max], f[max], i, j, nb, nf, temp;
static int bf[max], ff[max];

printf("Memory 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 = 0; i < nb; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &b[i]);
bf[i] = 0;
}

printf("\nEnter 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 < nf; i++) {


for(j = 0; j < nb; j++) {
if(bf[j] == 0) {
temp = b[j] - f[i];
if(temp >= 0) {
ff[i] = j;
frag[i] = temp;
bf[j] = 1;
break;
}
}
}
}

printf("\nFile_no:\tFile_size:\tBlock_no:\tBlock_size:\tFragment\n");
for(i = 0; i < nf; i++) {
if(ff[i] >= 0) {
printf("\n%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("\n%d\t\t%d\t\tNot Allocated", i + 1, f[i]);
}
}

return 0;
}

Output:

You might also like