exp7]
exp7]
Fit)
Theory:-
1) Best Fit
The best-fit memory allocation technique is a strategy used by operating systems to assign memory
to processes requesting it. It aims to minimize internal fragmentation and maximize memory
utilization efficiency.
● Finding the Memory Block: When a process requests memory, the operating system
searches through the list of free memory blocks.
● Selecting the "Best" Block: Instead of choosing the first block that fits (First-Fit) or the
smallest block available (Worst-Fit), best-fit seeks the smallest block that is still larger than
the process's memory requirement. This leaves minimal unused space (internal
fragmentation) in the allocated block.
● Allocation and Management: The chosen memory block is assigned to the process, and the
remaining space can be used for other allocations later. The operating system keeps track of
allocated and free memory blocks to manage future requests efficiently.
2) First Fit
The first-fit memory allocation technique is a simple and efficient strategy used by operating systems
to assign memory to processes requesting it.
● Finding the Memory Block: When a process requests memory, the operating system starts
searching through the list of free memory blocks, typically from the beginning.
● Matching the Need: The first block encountered that is at least as large as the process's
memory requirement is chosen for allocation. If no suitable block is found before reaching
the end of the list, the request fails.
● Allocation and Management: The chosen block is assigned to the process, and the remaining
space (if any) becomes a new free block, available for future allocations. The operating
system keeps track of allocated and free memory blocks to manage future requests
efficiently.
3) Worst Fit
The worst-fit memory allocation technique takes a different approach compared to First-Fit and Best-
Fit. Instead of prioritizing speedy allocation or minimizing fragmentation, it focuses on maximizing
the utilization of large memory blocks.
● Finding the Memory Block: When a process requests memory, the operating system
searches through the list of free memory blocks.
● Selecting the "Worst" Block: Contrary to its name, "worst" in this context refers to the
largest free block that can still accommodate the process's memory requirement. This leaves
the smallest remaining unused space in the chosen block.
● Allocation and Management: The chosen block is assigned to the process, and the remaining
space becomes a new free block, available for future allocations. The operating system
maintains track of allocated and free memory blocks.
Program:-
#include <stdio.h>
#include <string.h>
void WorstFit(int blocks[], int processes[], int b, int p) {
int allocid[p];
memset(allocid, -1, sizeof(allocid));
if (max != -1) {
allocid[i] = max;
blocks[max] -= processes[i];
}
}
printf("\nWorst Fit Algorithm:\n\n");
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < p; i++) {
printf("%d\t\t%d\t\t", i + 1, processes[i]);
if (allocid[i] != -1) {
printf("%d", allocid[i] + 1); // Adding 1 to make block numbers 1-indexed
} else {
printf("Not Allocated");
}
printf("\n");
}
}
if (min != -1) {
allocid[i] = min;
blocks[min] -= processes[i];
}
}
printf("\nBest Fit Algorithm:\n\n");
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < p; i++) {
printf("%d\t\t%d\t\t", i + 1, processes[i]);
if (allocid[i] != -1) {
printf("%d", allocid[i] + 1);
} else {
printf("Not Allocated");
}
printf("\n");
}
}
int main() {
int b, p, k;
printf("Choose algorithm (1: Worst Fit, 2: First Fit, 3: Best Fit): ");
scanf("%d", &k);
if (k == 1) {
WorstFit(blocks, processes, b, p);
} else if (k == 2) {
FirstFit(blocks, processes, b, p);
} else if (k == 3) {
BestFit(blocks, processes, b, p);
} else {
printf("Invalid choice.\n");
}
return 0;
}
Output:-
Worst Fit
First Fit
First Fit
Best Fit
Conclusion:-
Memory allocation techniques have been correctly implemented.