0% found this document useful (0 votes)
3 views3 pages

11B Best Fit Program:: Sprno:9223

The document contains a C program that implements the Best Fit memory allocation algorithm. It prompts the user to enter the number of memory blocks and their sizes, as well as the number of processes and their sizes, then allocates memory blocks to processes based on the best fit strategy. The output displays the allocation results and the remaining memory in each block.

Uploaded by

jayaprakash9223
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)
3 views3 pages

11B Best Fit Program:: Sprno:9223

The document contains a C program that implements the Best Fit memory allocation algorithm. It prompts the user to enter the number of memory blocks and their sizes, as well as the number of processes and their sizes, then allocates memory blocks to processes based on the best fit strategy. The output displays the allocation results and the remaining memory in each block.

Uploaded by

jayaprakash9223
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/ 3

Sprno:9223

11B BEST FIT


PROGRAM:
#include <stdio.h>
#define MAX_BLOCKS 20
#define MAX_PROCESSES 20
void bestFit(int blockSize[], int blocks, int processSize[], int processes) {
int allocation[MAX_PROCESSES];
// Initialize all allocations to -1
for (int i = 0; i < processes; i++) {
allocation[i] = -1;
}
// Best Fit Allocation
for (int i = 0; i < processes; i++) {
int bestIndex = -1;
for (int j = 0; j < blocks; j++) {
if (blockSize[j] >= processSize[i]) {
if (bestIndex == -1 || blockSize[j] < blockSize[bestIndex]) {
bestIndex = j;
}
}
}
// Allocate if a suitable block is found
if (bestIndex != -1) {
allocation[i] = bestIndex;
blockSize[bestIndex] -= processSize[i];
}
}
// Display allocation results
printf("\nProcess\tSize\tAllocated Block\n");
for (int i = 0; i < processes; i++) {
Sprno:9223

printf("P%d\t%d\t", i + 1, processSize[i]);
if (allocation[i] != -1)
printf("Block %d\n", allocation[i] + 1);
else
printf("Not Allocated\n");
}
// Display remaining sizes in blocks
printf("\nRemaining Memory in Blocks:\n");
for (int i = 0; i < blocks; i++) {
printf("Block %d: %d\n", i + 1, blockSize[i]);
}
}
int main() {
int blockSize[MAX_BLOCKS], processSize[MAX_PROCESSES];
int blocks, processes;
printf("Enter number of memory blocks: ");
scanf("%d", &blocks);
printf("Enter the sizes of the %d blocks:\n", blocks);
for (int i = 0; i < blocks; i++) {
scanf("%d", &blockSize[i]);
}
printf("Enter number of processes: ");
scanf("%d", &processes);
printf("Enter the sizes of the %d processes:\n", processes);
for (int i = 0; i < processes; i++) {
scanf("%d", &processSize[i]);
}
bestFit(blockSize, blocks, processSize, processes);
return 0;
}
Sprno:9223

OUTPUT:
Enter number of memory blocks: 5
Enter the sizes of the 5 blocks:
100 500 200 300 600
Enter number of processes: 3
Enter the sizes of the 3 processes:
212 417 112

Process Size Allocated Block


P1 212 Block 4
P2 417 Block 2
P3 112 Block 3

Remaining Memory in Blocks:


Block 1: 100
Block 2: 83
Block 3: 88
Block 4: 88
Block 5: 600

You might also like