Contiguous Memory Allocation
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a memory block
struct MemoryBlock {
int start;
int size;
struct MemoryBlock* next;
};
// Function to display the memory allocation status
void displayMemory(struct MemoryBlock* memory) {
printf("Memory Allocation Status:\n");
while (memory != NULL) {
printf("Start: %d, Size: %d\n", memory->start, memory->size);
memory = memory->next;
}
printf("\n");
}
// Function to allocate memory using Worst Fit
void worstFit(struct MemoryBlock** memory, int processSize) {
struct MemoryBlock* current = *memory;
struct MemoryBlock* worstFitBlock = NULL;
while (current != NULL) {
if (current->size >= processSize) {
if (worstFitBlock == NULL || current->size > worstFitBlock->size) {
worstFitBlock = current;
}
}
current = current->next;
}
if (worstFitBlock != NULL) {
// Allocate memory
struct MemoryBlock* newBlock = (struct MemoryBlock*)malloc(sizeof(struct MemoryBlock));
newBlock->start = worstFitBlock->start;
newBlock->size = processSize;
newBlock->next = worstFitBlock->next;
worstFitBlock->start += processSize;
worstFitBlock->size -= processSize;
worstFitBlock->next = newBlock;
printf("Memory allocated using Worst Fit.\n");
} else {
printf("Memory allocation failed using Worst Fit.\n");
}
}
// Function to allocate memory using Best Fit
void bestFit(struct MemoryBlock** memory, int processSize) {
struct MemoryBlock* current = *memory;
struct MemoryBlock* bestFitBlock = NULL;
while (current != NULL) {
if (current->size >= processSize) {
if (bestFitBlock == NULL || current->size < bestFitBlock->size) {
bestFitBlock = current;
}
}
current = current->next;
}
if (bestFitBlock != NULL) {
// Allocate memory
struct MemoryBlock* newBlock = (struct MemoryBlock*)malloc(sizeof(struct MemoryBlock));
newBlock->start = bestFitBlock->start;
newBlock->size = processSize;
newBlock->next = bestFitBlock->next;
bestFitBlock->start += processSize;
bestFitBlock->size -= processSize;
bestFitBlock->next = newBlock;
printf("Memory allocated using Best Fit.\n");
} else {
printf("Memory allocation failed using Best Fit.\n");
}
}
// Function to allocate memory using First Fit
void firstFit(struct MemoryBlock** memory, int processSize) {
struct MemoryBlock* current = *memory;
struct MemoryBlock* prev = NULL;
while (current != NULL) {
if (current->size >= processSize) {
// Allocate memory
struct MemoryBlock* newBlock = (struct MemoryBlock*)malloc(sizeof(struct MemoryBlock));
newBlock->start = current->start;
newBlock->size = processSize;
newBlock->next = current->next;
if (prev == NULL) {
*memory = newBlock;
} else {
prev->next = newBlock;
}
current->start += processSize;
current->size -= processSize;
printf("Memory allocated using First Fit.\n");
return;
}
prev = current;
current = current->next;
}
printf("Memory allocation failed using First Fit.\n");
}
// Function to free memory block
void deallocateMemory(struct MemoryBlock** memory, int processStart, int processSize) {
struct MemoryBlock* current = *memory;
struct MemoryBlock* prev = NULL;
// Find the memory block to deallocate
while (current != NULL && (current->start + current->size) != processStart) {
prev = current;
current = current->next;
}
if (current != NULL) {
// Deallocate memory
struct MemoryBlock* newBlock = (struct MemoryBlock*)malloc(sizeof(struct MemoryBlock));
newBlock->start = processStart;
newBlock->size = processSize;
newBlock->next = current->next;
if (prev == NULL) {
*memory = newBlock;
} else {
prev->next = newBlock;
}
printf("Memory deallocated.\n");
} else {
printf("Memory deallocation failed. Block not found.\n");
}
}
// Function to free all allocated memory blocks
void freeMemory(struct MemoryBlock* memory) {
while (memory != NULL) {
struct MemoryBlock* temp = memory;
memory = memory->next;
free(temp);
}
}
int main() {
struct MemoryBlock* memory = (struct MemoryBlock*)malloc(sizeof(struct MemoryBlock));
memory->start = 0;
memory->size = 1000;
memory->next = NULL;
int choice, processSize, processStart;
do {
printf("1. Worst Fit\n");
printf("2. Best Fit\n");
printf("3. First Fit\n");
printf("4. Display Memory Status\n");
printf("5. Deallocate Memory\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter process size: ");
scanf("%d", &processSize);
worstFit(&memory, processSize);
break;
case 2:
printf("Enter process size: ");
scanf("%d", &processSize);
bestFit(&memory, processSize);
break;
case 3:
printf("Enter process size: ");
scanf("%d", &processSize);
firstFit(&memory, processSize);
break;
case 4:
displayMemory(memory);
break;
case 5:
printf("Enter process start and size to deallocate: ");
scanf("%d %d", &processStart, &processSize);
deallocateMemory(&memory, processStart, processSize);
break;
case 6:
freeMemory(memory);
printf("Program exited.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 6);
return 0;
}