OSEXP8
OSEXP8
8
Memory Management
A. Write a program to demonstrate the concept of dynamic
partitioning placement algorithms i.e. Best Fit, First Fit,
Worst-Fit
Experiment No. 8
Aim: To study and implement memory allocation strategy First fit.
1
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
Objective:
Write a program to demonstrate the concept of dynamic partitioning placement
algorithms
i.e. Best Fit, First Fit, Worst-Fit etc.
Theory:
The primary role of the memory management system is to satisfy requests for memory
allocation. Sometimes this is implicit, as when a new process is created. At other
times, processes explicitly request memory. Either way, the system must locate
enough unallocated memory and assign it to the process.
Partitioning: The simplest methods of allocating memory are based on dividing
memory into areas with fixed partitions.
Selection Policies: If more than one free block can satisfy a request, then which one
should we pick? There are several schemes that are frequently studied and are
commonly used.
⦁ First Fit: In the first fit approach is to allocate the first free partition or hole
large enough which can accommodate the process. It finishes after finding the
first suitable free partition.
⦁ Advantage: Fastest algorithm because it searches as little as possible.
⦁ Disadvantage: The remaining unused memory areas left after allocation
become waste if it is too smaller. Thus request for larger memory
requirement cannot be accomplished
⦁ Best Fit: The best fit deals with allocating the smallest free partition which
meets the requirement of the requesting process. This algorithm first searches
the entire list of free partitions and considers the smallest hole that is adequate.
It then tries to find a hole which is close to actual process size needed.
⦁ Worst fit: In worst fit approach is to locate largest available free portion so
that the portion left will be big enough to be useful. It is the reverse of best fit.
⦁ Next Fit: If we want to spread the allocations out more evenly across the
memory
2
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
space, we often use a policy called next fit. This scheme is very similar to the
first fit approach, except for the place where the search starts.
Program:
#include <stdio.h>
#include <stdlib.h>
typedef struct
MemoryBlock { int
start_address;
int size;
int
allocat
ed; int
process
_id;
struct MemoryBlock* next;
} MemoryBlock;
MemoryBlock* head =
NULL;
void insertMemoryBlock(int start_address, int size) {
MemoryBlock* newBlock = (MemoryBlock*)
malloc(sizeof(MemoryBlock)); newBlock->start_address =
start_address;
newBlock->size =
size; newBlock->
3
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
allocated = 0;
newBlock->
process_id = -1;
newBlock->next =
NULL;
if (head ==
NULL) { head
= newBlock;
} else {
MemoryBlock* temp =
head; while (temp->
next != NULL) {
temp = temp->next;
}
temp->next = newBlock;
}
void displayMemory()
{ MemoryBlock*
temp = head;
printf("Memory
Blocks:\n"); while
(temp != NULL) {
printf("[Start: %d, Size: %d, Allocated: %d, Process: %d]\n",
temp->start_address, temp->size, temp->allocated, temp->
process_id); temp = temp->next;
}
printf("\n");
4
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
}
return 0; // Failure
}
5
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
process_id) { MemoryBlock*
temp = head; MemoryBlock*
bestBlock = NULL; while (temp !
= NULL) {
if (!temp->allocated && temp->size >= process_size) {
if (bestBlock == NULL || temp->size < bestBlock->size)
{ bestBlock = temp;
}
}
temp = temp->next;
}
if (bestBlock !=
NULL)
{ bestBlock->
allocated = 1;
bestBlock->process_id =
process_id; if (bestBlock->
size > process_size) {
MemoryBlock* newBlock = (MemoryBlock*)
malloc(sizeof(MemoryBlock)); newBlock->start_address =
bestBlock->start_address + process_size; newBlock->size =
bestBlock->size - process_size;
newBlock->allocated = 0;
newBlock->process_id = -1;
newBlock->next =
bestBlock->next;
bestBlock->size =
process_size; bestBlock->
next = newBlock;
6
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
}
return 1;
}
return 0;
if (worstBlock !=
NULL)
{ worstBlock->
allocated = 1;
worstBlock->process_id =
process_id; if (worstBlock->
size > process_size) {
MemoryBlock* newBlock = (MemoryBlock*)
malloc(sizeof(MemoryBlock)); newBlock->start_address =
worstBlock->start_address + process_size; newBlock->size =
worstBlock->size - process_size;
newBlock->allocated = 0;
7
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
newBlock->process_id = -1;
newBlock->next =
worstBlock->next;
worstBlock->size =
process_size; worstBlock->
next = newBlock;
}
return 1;
}
return 0;
}
int main()
{ insertMemoryBlock(
0, 100);
insertMemoryBlock(100, 50);
insertMemoryBlock(150, 200);
insertMemoryBlock(350, 75);
int processes[][2] = {{30, 1}, {60, 2}, {100, 3}, {35, 4}};
int numProcesses = sizeof(processes) / sizeof(processes[0]);
printf("Initial
Memory:\n");
displayMemory();
printf("\nFirstFit Allocation:\n");
MemoryBlock* originalHead = head; //save the first head.
printf("\nBestFit Allocation:\n");
for (int i = 0; i < numProcesses; i++) {
if (bestFit(processes[i][0], processes[i][1])) {
printf("Allocated %d for Process %d\n", processes[i][0], processes[i][1]);
} else {
printf("Failed to allocate %d for Process %d\n", processes[i][0], processes[i]
[1]);
}
}
displayMemory();
head = originalHead; //reset head for next algorithm.
printf("\nWorstFit Allocation:
\n"); for (int i = 0; i
< numProcesses; i++) {
if (worstFit(processes[i][0], processes[i][1])) {
printf("Allocated %d for Process %d\n", processes[i][0], processes[i][1]);
} else {
printf("Failed to allocate %d for Process %d\n", processes[i][0], processes[i]
[1]);
}
}
9
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
displayMemory();
return 0;
}
//Output
Initial Memory:
Memory Blocks:
[Start: 0, Size: 100, Allocated: 0, Process: -1]
[Start: 100, Size: 50, Allocated: 0, Process: -1]
[Start: 150, Size: 200, Allocated: 0, Process: -1]
[Start: 350, Size: 75, Allocated: 0, Process: -1]
FirstFit Allocation:
Allocated 30 for Process 1
Allocated 60 for Process 2
Allocated 100 for Process 3
Allocated 35 for
Process 4 Memory
10
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
Blocks:
[Start: 0, Size: 30, Allocated: 1, Process: 1]
[Start: 30, Size: 60, Allocated: 1, Process: 2]
[Start: 90, Size: 10, Allocated: 0, Process: -1]
[Start: 100, Size: 35, Allocated: 1, Process: 4]
[Start: 135, Size: 15, Allocated: 0, Process: -1]
[Start: 150, Size: 100, Allocated: 1, Process: 3]
[Start: 250, Size: 100, Allocated: 0, Process: -1]
[Start: 350, Size: 75, Allocated: 0, Process: -1]
BestFit Allocation:
Allocated 30 for Process 1
Allocated 60 for Process 2
Failed to allocate 100 for
Process 3 Allocated 35 for
Process 4 Memory Blocks:
[Start: 0, Size: 30, Allocated: 1, Process: 1]
[Start: 30, Size: 60, Allocated: 1, Process: 2]
[Start: 90, Size: 10, Allocated: 0, Process: -1]
[Start: 100, Size: 35, Allocated: 1, Process: 4]
[Start: 135, Size: 15, Allocated: 0, Process: -1]
[Start: 150, Size: 100, Allocated: 1, Process: 3]
[Start: 250, Size: 60, Allocated: 1, Process: 2]
[Start: 310, Size: 35, Allocated: 1, Process: 4]
11
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
WorstFit Allocation:
Allocated 30 for Process 1
Failed to allocate 60 for
Process 2 Failed to allocate
100 for Process 3 Failed to
allocate 35 for Process 4
Memory Blocks:
[Start: 0, Size: 30, Allocated: 1, Process: 1]
[Start: 30, Size: 60, Allocated: 1, Process: 2]
[Start: 90, Size: 10, Allocated: 0, Process: -1]
[Start: 100, Size: 35, Allocated: 1, Process: 4]
[Start: 135, Size: 15, Allocated: 0, Process: -1]
[Start: 150, Size: 100, Allocated: 1, Process: 3]
[Start: 250, Size: 60, Allocated: 1, Process: 2]
[Start: 310, Size: 35, Allocated: 1, Process: 4]
[Start: 345, Size: 5, Allocated: 0, Process: -1]
[Start: 350, Size: 30, Allocated: 1, Process: 1]
[Start: 380, Size: 30, Allocated: 1, Process: 1]
[Start: 410, Size: 15, Allocated: 0, Process: -1]
Conclusion:
12
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
⦁
13
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.
⦁ Enabling Multitasking:
⦁ Modern operating systems support multitasking, which involves
running multiple programs concurrently.
⦁
14
This PDF document was edited with Icecream PDF Editor.
Upgrade to PRO to remove watermark.