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

exp7]

The document outlines a program that implements three contiguous memory allocation techniques: Best Fit, First Fit, and Worst Fit. Each technique is explained in terms of how it allocates memory to processes, aiming to optimize memory utilization and minimize fragmentation. The program includes functions for each allocation method and allows user input for memory blocks and process sizes.

Uploaded by

iyxlo126mr
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)
5 views5 pages

exp7]

The document outlines a program that implements three contiguous memory allocation techniques: Best Fit, First Fit, and Worst Fit. Each technique is explained in terms of how it allocates memory to processes, aiming to optimize memory utilization and minimize fragmentation. The program includes functions for each allocation method and allows user input for memory blocks and process sizes.

Uploaded by

iyxlo126mr
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

Aim: Write a program to implement contiguous memory allocation techniques (Best Fit, First Fit and Worst

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));

for (int i = 0; i < p; i++) {


int max = -1;
for (int j = 0; j < b; j++) {
if (blocks[j] >= processes[i]) {
if (max == -1 || blocks[max] < blocks[j]) {
max = j;
}
}
}

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");
}
}

void FirstFit(int blocks[], int processes[], int b, int p){


int allocid[p];
memset(allocid, -1, sizeof(allocid));
for(int i=0;i<p;i++){
for(int j = 0;j<b;j++){
if (blocks[j]>=processes[i]){
allocid[i] = j;
blocks[j] = blocks[j] - processes[i];
break;
}
}
}
printf("\nFirst 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");
}
}
void BestFit(int blocks[], int processes[], int b, int p) {
int allocid[p];
memset(allocid, -1, sizeof(allocid));

for (int i = 0; i < p; i++) {


int min = -1;
for (int j = 0; j < b; j++) {
if (blocks[j] >= processes[i]) {
if (min == -1 || blocks[min] > blocks[j]) {
min = j;
}
}
}

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("Enter number of memory blocks: ");


scanf("%d", &b);

printf("Enter number of processes: ");


scanf("%d", &p);

int blocks[b], processes[p];

printf("Enter sizes for memory blocks:\n");


for (int i = 0; i < b; i++) {
printf("Block %d: ", i + 1);
scanf("%d", &blocks[i]);
}

printf("Enter sizes for processes:\n");


for (int i = 0; i < p; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &processes[i]);
}

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.

You might also like