Professional Documents
Culture Documents
Untitled Document
Untitled Document
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>
int memory[MEMORY_SIZE];
int lastAllocatedIndex = 0;
void initializeMemory() {
for (int i = 0; i < MEMORY_SIZE; i++) {
memory[i] = -1;
}
}
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
}
if (bestFitIndex != -1) {
for (int i = bestFitIndex; i < bestFitIndex + size; i++) {
memory[i] = size;
}
}
UI22CS57 LAB 9 TEJKUMAR
return bestFitIndex;
}
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