You are on page 1of 7

UI22CS57 LAB 9 TEJKUMAR

AIM:- Write a program to implement First-Fit, Best-Fit and Next-fit.

BEST FIT ALGORITHM-:


● nitialization: The available memory is divided into different-sized blocks or partitions.
These partitions can be fixed or variable in size.
● Memory Request: When a process or data structure requires memory, it makes a
request for a certain amount of memory space.
● Search for Best Fit: The algorithm searches for the smallest available memory block that
can accommodate the requested size. It tries to find the block that is the closest in size
to the requested memory, hence the name "best fit."
● Allocation: If a suitable block is found, the requested memory is allocated in that block. If
the block is larger than the requested memory, it may be split into two parts, with one
part allocated to the process, and the other part remaining as unallocated memory.
● Release or Deallocate: When a process no longer needs the allocated memory, it
releases the memory, which becomes available for allocation again.

FIRST FIT ALGORITHM-:


● Initialization: The available memory is divided into different-sized blocks or partitions,
which can be fixed or variable in size.
● Memory Request: When a process or data structure requires memory, it makes a
request for a certain amount of memory space.
● Search for First Fit: The algorithm starts searching from the beginning of the available
memory and looks for the first partition that is large enough to accommodate the
requested memory size. It allocates the process in the first partition that is large enough
to fit.
● Allocation: If a suitable block is found, the requested memory is allocated in that block. If
the block is larger than the requested memory, it may be split into two parts, with one
part allocated to the process, and the other part remaining as unallocated memory.
● Release or Deallocate: When a process no longer needs the allocated memory, it
releases the memory, and the previously allocated block becomes available for
allocation again.

NEXT ALGORITHM-:
● Initialization: The available memory is divided into different-sized blocks or partitions,
which can be fixed or variable in size.
● Memory Request: When a process or data structure requires memory, it makes a
request for a certain amount of memory space.
● Search for Next Fit: The algorithm maintains a pointer to the last allocated memory
block. When a new memory request arrives, it starts searching from the block
immediately following the last allocated block. It looks for the next partition that is large
enough to accommodate the requested memory size.
UI22CS57 LAB 9 TEJKUMAR

● Allocation: If a suitable block is found, the requested memory is allocated in that block. If
the block is larger than the requested memory, it may be split into two parts, with one
part allocated to the process, and the other part remaining as unallocated memory.
● Update the Pointer: After allocation, the pointer is updated to point to the block where
the memory allocation occurred. This pointer helps to continue searching for the next fit
from where the last allocation took place.
● Release or Deallocate: When a process no longer needs the allocated memory, it
releases the memory, and the previously allocated block becomes available for
allocation again.

NOW LET US TRY WITH AN CODE WITRH AN CASES OF FIRST FIT BEST FIT AND NEXT
FIT-:

Input-:

#include <stdio.h>

#define MEMORY_SIZE 100

int memory[MEMORY_SIZE];
int lastAllocatedIndex = 0;

void initializeMemory() {
for (int i = 0; i < MEMORY_SIZE; i++) {
memory[i] = -1;
}
}

// Display the current memory allocation


void displayMemory() {
for (int i = 0; i < MEMORY_SIZE; i++) {
if (memory[i] == -1) {
printf("- ");
} else {
printf("%d ", memory[i]);
}
}
printf("\n");
}

// First Fit allocation strategy


int firstFit(int size) {
for (int i = 0; i < MEMORY_SIZE; i++) {
UI22CS57 LAB 9 TEJKUMAR

int blockSize = 0;
if (memory[i] == -1) {
while (i < MEMORY_SIZE && memory[i] == -1) {
blockSize++;
i++;
}
if (blockSize >= size) {
for (int j = i - blockSize; j < i; j++) {
memory[j] = size;
}
return i - blockSize;
}
}
}
return -1; // Not enough memory available
}

// Best Fit allocation strategy


int bestFit(int size) {
int bestFitIndex = -1;
int minBlockSize = MEMORY_SIZE + 1;
int blockSize = 0;

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


if (memory[i] == -1) {
blockSize++;
} else {
if (blockSize >= size && blockSize < minBlockSize) {
bestFitIndex = i - blockSize;
minBlockSize = blockSize;
}
blockSize = 0;
}
}

if (blockSize >= size && blockSize < minBlockSize) {


bestFitIndex = MEMORY_SIZE - blockSize;
}

if (bestFitIndex != -1) {
for (int i = bestFitIndex; i < bestFitIndex + size; i++) {
memory[i] = size;
}
}
UI22CS57 LAB 9 TEJKUMAR

return bestFitIndex;
}

// Next Fit allocation strategy


int nextFit(int size) {
for (int i = lastAllocatedIndex; i < MEMORY_SIZE; i++) {
int blockSize = 0;
if (memory[i] == -1) {
while (i < MEMORY_SIZE && memory[i] == -1) {
blockSize++;
i++;
}
if (blockSize >= size) {
for (int j = i - blockSize; j < i; j++) {
memory[j] = size;
}
lastAllocatedIndex = i;
return i - blockSize;
}
}
}
for (int i = 0; i < lastAllocatedIndex; i++) {
int blockSize = 0;
if (memory[i] == -1) {
while (i < lastAllocatedIndex && memory[i] == -1) {
blockSize++;
i++;
}
if (blockSize >= size) {
for (int j = i - blockSize; j < i; j++) {
memory[j] = size;
}
lastAllocatedIndex = i;
return i - blockSize;
}
}
}
return -1; // Not enough memory available
}

int main() {
int choice, size, index;

initializeMemory();
UI22CS57 LAB 9 TEJKUMAR

while (1) {
printf("\nMemory Allocation Strategies:\n");
printf("1. First Fit\n");
printf("2. Best Fit\n");
printf("3. Next Fit\n");
printf("4. Display Memory\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the size of the process: ");
scanf("%d", &size);
index = firstFit(size);
if (index != -1) {
printf("Memory allocated at position %d.\n", index);
} else {
printf("Memory allocation failed. Not enough memory available.\n");
}
break;
case 2:
printf("Enter the size of the process: ");
scanf("%d", &size);
index = bestFit(size);
if (index != -1) {
printf("Memory allocated at position %d.\n", index);
} else {
printf("Memory allocation failed. Not enough memory available.\n");
}
break;
case 3:
printf("Enter the size of the process: ");
scanf("%d", &size);
index = nextFit(size);
if (index != -1) {
printf("Memory allocated at position %d.\n", index);
} else {
printf("Memory allocation failed. Not enough memory available.\n");
}
break;
case 4:
displayMemory();
UI22CS57 LAB 9 TEJKUMAR

break;
case 5:
return 0;
default:
printf("Invalid choice.\n");
}
}

return 0;
}

OUTPUT-:
UI22CS57 LAB 9 TEJKUMAR

You might also like