0% found this document useful (0 votes)
18 views10 pages

os exp8 b03

Uploaded by

vedantshimpi3114
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)
18 views10 pages

os exp8 b03

Uploaded by

vedantshimpi3114
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/ 10

Experiment No-08

PART A
(PART A: TO BE REFFERED BY STUDENTS)

A.1 Memory Management


a. Write a program to demonstrate the concept of MVT and MFT memory management
techniques
b. Write a program to demonstrate the concept of dynamic partitioning placement algorithms
i.e. Best Fit, First Fit, Worst-Fit etc.

A-2 Prerequisite
Knowledge about C/C++ programming language. A.3
Outcome
A.4 Theory:
a. Concept of MVT and MFT memory management techniques
MFT (Multiprogramming with a Fixed number of Tasks) is one of the old memory management
techniques in which the memory is partitioned into fixed size partitions and each job is assigned
to a partition. The memory assigned to a partition does not change.

MVT (Multiprogramming with a Variable number of Tasks) is the memory management technique
in which each job gets just the amount of memory it needs. That is, the partitioning of memory is
dynamic and changes as jobs enter and leave the system. MVT is a more ``efficient'' user of
resources. MFT suffers with the problem of internal fragmentation and MVT suffers with external
fragmentation.

b. Concept of dynamic partitioning placement algorithms i.e. Best Fit, First Fit, Worst-Fit
etc.
 First Fit:
In dynamic memory management, First Fit algorithm states that main memory (RAM) is
divided into many partitions of unequal sizes. When processes are generated in system,
they need to be placed in different partitions of memory. In this method while placing a
process, the first partition which is large enough to accommodate the process is selected.
Hence first free and sufficient size partition is used to place the process.
1. It is fast method since all partitions are not searched for placing a process.
2. Since first partition is selected there is increase in internal fragmentation.
2K 1

3K 2

3K 3

2K 4
.

Processes: P1 size 3K, P2 size 2K, P3 size 2K, P4 size 1K


Sequentially Placed in Partitions 2,1,3,4

 Best Fit:
In dynamic memory management, Best Fit algorithm states that main memory (RAM) is
divided into many partitions of unequal sizes. When processes are generated in system,
they need to be placed in different partitions of memory. In this method while placing a
process, the first partition which is smallest of all which can to accommodate the process
is selected. Hence in all free and smallest size partition is used to place the process.
1. It is not faster method since all partitions are searched for placing a process.
2. Since smallest partition is selected there is great reduction in internal fragmentation.
.
2K 1

3K 2

3K 3

2K 4
Processes: P1 size 3K, P2 size 2K, P3 size 2K, P4 size 1K
Sequentially Placed in Partitions 2,1,4,3

 Worst Fit:
In dynamic memory management, Worst Fit algorithm states that main memory (RAM) is
divided into many partitions of unequal sizes. When processes are generated in system,
they need to be placed in different partitions of memory. In this method while placing a
process, the first partition which is largest in all to is selected. Hence first free and largest
size partition is used to place the process.
3. It is not fast method since all partitions are searched for placing a process.
4. Since largest partition is selected there is increase in internal and external
fragmentation.
. 2K 1

3K 2

3K 3

2K 4
Processes: P1 size 3K, P2 size 2K, P3 size 2K, P4 size 1K
Sequentially Placed in Partitions 2,3,1,4

PART B
(PART B: TO BE COMPLETED BY STUDENTS)

Roll. No: B03 Name: Vedant Gopal Shimpi


Class : S.E. B [ comps ] Batch: B1
Date of Experiment: 2/03/24 Date of Submission: 27/03/2024
Grade:

B.1 Software Code written by student:


(Paste your Search material completed during the 2 hours of practical in the lab here)

MVT and MFT Memory Management Techniques :

#include <stdio.h>
#include <stdbool.h>

#define MEMORY_SIZE 100

struct MemoryBlock {
int start;
int size; int
process;
};

typedef struct MemoryBlock MemoryBlock;

MemoryBlock memory[MEMORY_SIZE];

void initializeMemory() {
memory[0].start = 0;
memory[0].size = MEMORY_SIZE;
memory[0].process = -1; // -1 indicates free memory }
void printMemory() {
printf("Memory:\n");
for (int i = 0; i < MEMORY_SIZE; i++) {
printf("[%d:%d] ", memory[i].start, memory[i].size);
}
printf("\n");
}

bool allocateMVT(int process_id, int size) {


for (int i = 0; i < MEMORY_SIZE; i++) {
if (memory[i].process == -1 && memory[i].size >= size) {
memory[i].process = process_id;
if (memory[i].size > size) {
memory[i + 1].start = memory[i].start + size;
memory[i + 1].size = memory[i].size - size;
memory[i].size = size;
}
return true;
} }
return false;
}

bool allocateMFT(int process_id, int size) { for (int i = 0; i <


MEMORY_SIZE; i++) { if (memory[i].process == -1 &&
memory[i].size >= size) {
memory[i].process = process_id;
return true;
} }
return false;
}

void deallocate(int process_id) { for (int i = 0; i <


MEMORY_SIZE; i++) { if (memory[i].process
== process_id) { memory[i].process = -1;
// Merge adjacent free blocks if (i > 0 &&
memory[i - 1].process == -1) { memory[i -
1].size += memory[i].size; for (int j = i; j <
MEMORY_SIZE - 1; j++) { memory[j] =
memory[j + 1];
}
}
if (i < MEMORY_SIZE - 1 && memory[i + 1].process == -1) {
memory[i].size += memory[i + 1].size;
for (int j = i + 1; j < MEMORY_SIZE - 1; j++) {
memory[j] = memory[j + 1];
}
}
break;
}
}
}

int main() {
initializeMemory();
printMemory();

printf("\nAllocating process 1 using MVT with size 20...\n");


if (allocateMVT(1, 20)) { printMemory(); } else {
printf("Allocation failed.\n");
}

printf("\nAllocating process 2 using MFT with size 30...\n");


if (allocateMFT(2, 30)) { printMemory(); } else {
printf("Allocation failed.\n");
}

printf("\nAllocating process 3 using MVT with size 10...\n");


if (allocateMVT(3, 10)) { printMemory();
} else {
printf("Allocation failed.\n");
}

printf("\nDeallocating process 1...\n");


deallocate(1);
printMemory();

return 0;
}

Dynamic Partitioning :

#include <stdio.h>
#include <stdlib.h>

#define MEMORY_SIZE 100

struct MemoryBlock {
int start;
int size; int
process;
};

typedef struct MemoryBlock MemoryBlock;

MemoryBlock memory[MEMORY_SIZE];

void initializeMemory() {
memory[0].start = 0;
memory[0].size = MEMORY_SIZE;
memory[0].process = -1; // -1 indicates free memory
}

void printMemory() {
printf("Memory:\n");
for (int i = 0; i < MEMORY_SIZE; i++) {
printf("[%d:%d] ", memory[i].start, memory[i].size);
}
printf("\n");
}
int findBestFitIndex(int size) {
int best_fit_index = -1; int min_size =
MEMORY_SIZE + 1; for (int i = 0; i <
MEMORY_SIZE; i++) {
if (memory[i].process == -1 && memory[i].size >= size && memory[i].size < min_size) {
best_fit_index = i; min_size = memory[i].size;
}
}
return best_fit_index;
}

int findFirstFitIndex(int size) { for (int i =


0; i < MEMORY_SIZE; i++) {
if (memory[i].process == -1 && memory[i].size >= size) {
return i;
} }
return -1;
}

int findWorstFitIndex(int size) {


int worst_fit_index = -1; int
max_size = -1;
for (int i = 0; i < MEMORY_SIZE; i++) {
if (memory[i].process == -1 && memory[i].size >= size && memory[i].size > max_size) {
worst_fit_index = i; max_size = memory[i].size;
}
}
return worst_fit_index;
}

void allocate(int process_id, int size, int (*fitAlgorithm)(int)) {


int index = fitAlgorithm(size);
if (index != -1) {
memory[index].process = process_id; if
(memory[index].size > size) { memory[index +
1].start = memory[index].start + size; memory[index +
1].size = memory[index].size - size;
memory[index].size = size;
}
} else {
printf("Allocation failed for process %d with size %d.\n", process_id, size);
}
}

void deallocate(int process_id) { for (int i = 0; i <


MEMORY_SIZE; i++) { if (memory[i].process
== process_id) { memory[i].process = -1;
// Merge adjacent free blocks if (i > 0 &&
memory[i - 1].process == -1) { memory[i -
1].size += memory[i].size; for (int j = i; j <
MEMORY_SIZE - 1; j++) { memory[j] =
memory[j + 1];
}
}
if (i < MEMORY_SIZE - 1 && memory[i + 1].process == -1) {
memory[i].size += memory[i + 1].size;
for (int j = i + 1; j < MEMORY_SIZE - 1; j++) {
memory[j] = memory[j + 1];
}
}
break;
}
}
}

int main() {
initializeMemory();
printMemory();

printf("\nAllocating process 1 using Best Fit with size 20...\n");


allocate(1, 20, findBestFitIndex); printMemory();

printf("\nAllocating process 2 using First Fit with size 30...\n");


allocate(2, 30, findFirstFitIndex);
printMemory();

printf("\nAllocating process 3 using Worst Fit with size 10...\n");


allocate(3, 10, findWorstFitIndex); printMemory();

printf("\nDeallocating process 1...\n");


deallocate(1);
printMemory();

return 0;
}
B.2 Input and Output:
(input and output of the program)

MVT and MFT Technique :


Dynamic Partitioning :

B. Conclusion:

The provided C programs demonstrate different memory management techniques: Multiple


Variable Tasks (MVT), Multiple Fixed Tasks (MFT), and dynamic partitioning with Best Fit, First
Fit, and Worst Fit algorithms. These techniques offer various trade-offs in terms of memory
utilization, fragmentation, and simplicity. Understanding and implementing these strategies are
crucial for optimizing memory usage and system performance in computing environments.

B. Question of Curiosity
(To be answered by student based on the practical performed and learning/observations)
1. Which is more optimized method from First Fit, Best Fit and Worst Fit?

Best fit is slower than first fit as it must search the entire list every time. It has also be shown that
best fit performs worse than first fit as it tends to leave lots of small gaps. Worst Fit : As best fit
leaves many small, useless holes it might be a good idea to always use the largest hole available.

2. What is fragmentation? What do you mean Internal and External Fragmentation?

Internal Fragmentation occurs when a process needs more space than the size of allotted memory
block or use less space. External Fragmentation occurs when a process is removed from the main
memory. Solution. Best Fit Block Search is the solution for internal fragmentation.
3. What are the memory partitioning methods?

Memory partitioning means dividing the main memory into chunks of the same or different sizes
so that they can be assigned to processes in the main memory. There are two types of memory
partitioning techniques: Fixed-sized memory partitioning. Variable-sized memory partitioning.

4.Differentiate between first fit and best fit?

Both first-fit and best-fit strategies are good memory allocation strategies that allow the re-use of
memory. Both strategies have different strengths and weaknesses. Running Time: First-fit is
faster, allowing the searching for memory to stop immediately after finding a free-block of large
enough size.

You might also like