0% found this document useful (0 votes)
21 views7 pages

OSLExp 8

The document discusses three dynamic memory allocation algorithms: first fit, best fit, and worst fit. It provides code examples to demonstrate each algorithm and allocates memory based on the entered size. Each algorithm has trade-offs in terms of fragmentation and efficiency. The best strategy depends on the system's specific requirements.

Uploaded by

hydrasagar2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

OSLExp 8

The document discusses three dynamic memory allocation algorithms: first fit, best fit, and worst fit. It provides code examples to demonstrate each algorithm and allocates memory based on the entered size. Each algorithm has trade-offs in terms of fragmentation and efficiency. The best strategy depends on the system's specific requirements.

Uploaded by

hydrasagar2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Aim:-

Memory Management: WAP to demonstrate the concept of dynamic partitioning replacement


algorithms:

a. First Fit

Code:-
#include <stdio.h>

#define MEMORY_SIZE 100


#define MAX_PARTITIONS 10

typedef struct {
int start_address;
int end_address;
int size;
int allocated;
} Partition;

Partition memory[MAX_PARTITIONS];
int num_partitions = 0;

// Function to initialize memory


void initializeMemory() {
memory[0].start_address = 0;
memory[0].end_address = MEMORY_SIZE - 1;
memory[0].size = MEMORY_SIZE;
memory[0].allocated = 0;
num_partitions = 1;
}

// Function to display memory


void displayMemory() {
printf("Memory Layout:\n");
printf("Start Address\tEnd Address\tSize\tAllocated\n");
for (int i = 0; i < num_partitions; i++) {
printf("%d\t\t%d\t\t%d\t%s\n", memory[i].start_address, memory[i].end_address,
memory[i].size, memory[i].allocated ? "Yes" : "No");
}
printf("\n");
}

// Function to allocate memory using First Fit algorithm


void firstFit(int size) {
for (int i = 0; i < num_partitions; i++) {
if (!memory[i].allocated && memory[i].size >= size) {
memory[i].allocated = 1;
if (memory[i].size > size) {
memory[num_partitions].start_address = memory[i].start_address + size;
memory[num_partitions].end_address = memory[i].end_address;
memory[num_partitions].size = memory[i].size - size;
memory[num_partitions].allocated = 0;
num_partitions++;
}
memory[i].size = size;
printf("Memory allocated using First Fit.\n");
return;
}
}
printf("Memory allocation failed using First Fit.\n");
}

int main() {
int size;
initializeMemory();

printf("Enter memory size to allocate: ");


scanf("%d", &size);
firstFit(size);
displayMemory();

return 0;
}

Output:-
b. Best Fit:-

Code:-
#include <stdio.h>

#define MEMORY_SIZE 100


#define MAX_PARTITIONS 10

typedef struct {
int start_address;
int end_address;
int size;
int allocated;
} Partition;

Partition memory[MAX_PARTITIONS];
int num_partitions = 0;

// Function to initialize memory


void initializeMemory() {
memory[0].start_address = 0;
memory[0].end_address = MEMORY_SIZE - 1;
memory[0].size = MEMORY_SIZE;
memory[0].allocated = 0;
num_partitions = 1;
}

// Function to display memory


void displayMemory() {
printf("Memory Layout:\n");
printf("Start Address\tEnd Address\tSize\tAllocated\n");
for (int i = 0; i < num_partitions; i++) {
printf("%d\t\t%d\t\t%d\t%s\n", memory[i].start_address, memory[i].end_address,
memory[i].size, memory[i].allocated ? "Yes" : "No");
}
printf("\n");
}

// Function to allocate memory using Best Fit algorithm


void bestFit(int size) {
int best_fit_index = -1;
int min_fragmentation = MEMORY_SIZE + 1;

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


if (!memory[i].allocated && memory[i].size >= size) {
int fragmentation = memory[i].size - size;
if (fragmentation < min_fragmentation) {
min_fragmentation = fragmentation;
best_fit_index = i;
}
}
}
if (best_fit_index != -1) {
memory[best_fit_index].allocated = 1;
if (memory[best_fit_index].size > size) {
memory[num_partitions].start_address = memory[best_fit_index].start_address + size;
memory[num_partitions].end_address = memory[best_fit_index].end_address;
memory[num_partitions].size = memory[best_fit_index].size - size;
memory[num_partitions].allocated = 0;
num_partitions++;
}
memory[best_fit_index].size = size;
printf("Memory allocated using Best Fit.\n");
} else {
printf("Memory allocation failed using Best Fit.\n");
}
}

int main() {
int size;
initializeMemory();

printf("Enter memory size to allocate: ");


scanf("%d", &size);
bestFit(size);
displayMemory();

return 0;
}

Output:-
Worst Fit:-
Code:-
#include <stdio.h>

#define MEMORY_SIZE 100


#define MAX_PARTITIONS 10

typedef struct {
int start_address;
int end_address;
int size;
int allocated;
} Partition;

Partition memory[MAX_PARTITIONS];
int num_partitions = 0;

// Function to initialize memory


void initializeMemory() {
memory[0].start_address = 0;
memory[0].end_address = MEMORY_SIZE - 1;
memory[0].size = MEMORY_SIZE;
memory[0].allocated = 0;
num_partitions = 1;
}

// Function to display memory


void displayMemory() {
printf("Memory Layout:\n");
printf("Start Address\tEnd Address\tSize\tAllocated\n");
for (int i = 0; i < num_partitions; i++) {
printf("%d\t\t%d\t\t%d\t%s\n", memory[i].start_address, memory[i].end_address,
memory[i].size, memory[i].allocated ? "Yes" : "No");
}
printf("\n");
}

// Function to allocate memory using Worst Fit algorithm


void worstFit(int size) {
int worst_fit_index = -1;
int max_fragmentation = -1;

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


if (!memory[i].allocated && memory[i].size >= size) {
int fragmentation = memory[i].size - size;
if (fragmentation > max_fragmentation) {
max_fragmentation = fragmentation;
worst_fit_index = i;
}
}
}

if (worst_fit_index != -1) {
memory[worst_fit_index].allocated = 1;
if (memory[worst_fit_index].size > size) {
memory[num_partitions].start_address = memory[worst_fit_index].start_address + size;
memory[num_partitions].end_address = memory[worst_fit_index].end_address;
memory[num_partitions].size = memory[worst_fit_index].size - size;
memory[num_partitions].allocated = 0;
num_partitions++;
}
memory[worst_fit_index].size = size;
printf("Memory allocated using Worst Fit.\n");
} else {
printf("Memory allocation failed using Worst Fit.\n");
}
}

int main() {
int size;
initializeMemory();

printf("Enter memory size to allocate: ");


scanf("%d", &size);
worstFit(size);
displayMemory();

return 0;
}
Output:-

Conclusion:-In conclusion, each allocation strategy has its trade-offs in terms of fragmentation and
computational efficiency. The choice of strategy depends on the specific requirements of the
system, such as the expected size distribution of processes and the tolerance for fragmentation.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy