[go: up one dir, main page]

0% found this document useful (0 votes)
14 views2 pages

Memory Allocation

The document contains three C programs implementing memory allocation strategies: First Fit, Best Fit, and Worst Fit. Each program allocates memory blocks to processes based on the respective strategy and outputs the allocation results. The memory blocks and process sizes are predefined in the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Memory Allocation

The document contains three C programs implementing memory allocation strategies: First Fit, Best Fit, and Worst Fit. Each program allocates memory blocks to processes based on the respective strategy and outputs the allocation results. The memory blocks and process sizes are predefined in the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

First Fit – Simple Version


c
Copy
Edit
#include <stdio.h>
#include <conio.h>

void main() {
int block[5] = {100, 500, 200, 300, 600};
int process[4] = {212, 417, 112, 426};
int i, j;

clrscr();
printf("First Fit Allocation:\n");

for(i = 0; i < 4; i++) {


for(j = 0; j < 5; j++) {
if(block[j] >= process[i]) {
printf("Process %d of size %d allocated in block of size %d\n",
i+1, process[i], block[j]);
block[j] -= process[i];
break;
}
}
if(j == 5)
printf("Process %d of size %d not allocated\n", i+1, process[i]);
}

getch();
}
2. Best Fit – Simple Version
c
Copy
Edit
#include <stdio.h>
#include <conio.h>

void main() {
int block[5] = {100, 500, 200, 300, 600};
int process[4] = {212, 417, 112, 426};
int i, j, best;

clrscr();
printf("Best Fit Allocation:\n");

for(i = 0; i < 4; i++) {


best = -1;
for(j = 0; j < 5; j++) {
if(block[j] >= process[i]) {
if(best == -1 || block[j] < block[best])
best = j;
}
}
if(best != -1) {
printf("Process %d of size %d allocated in block of size %d\n", i+1,
process[i], block[best]);
block[best] -= process[i];
} else {
printf("Process %d of size %d not allocated\n", i+1, process[i]);
}
}

getch();
}
3. Worst Fit – Simple Version
c
Copy
Edit
#include <stdio.h>
#include <conio.h>

void main() {
int block[5] = {100, 500, 200, 300, 600};
int process[4] = {212, 417, 112, 426};
int i, j, worst;

clrscr();
printf("Worst Fit Allocation:\n");

for(i = 0; i < 4; i++) {


worst = -1;
for(j = 0; j < 5; j++) {
if(block[j] >= process[i]) {
if(worst == -1 || block[j] > block[worst])
worst = j;
}
}
if(worst != -1) {
printf("Process %d of size %d allocated in block of size %d\n", i+1,
process[i], block[worst]);
block[worst] -= process[i];
} else {
printf("Process %d of size %d not allocated\n", i+1, process[i]);
}
}

getch();
}

You might also like