Bscs4a Lab
Bscs4a Lab
Instructions:
● Apply the following scenario in both partitioning algorithms (Fixed Size and Variable
Size)
● You have to modify the given code in a way that it should implement the scenario and
print the output as shown.
● Below given code is for hint, you have to modify it as per the scenario given and print the
output as shown in example.
Scenario:
Consider a system with 1000 KB of memory and three processes with the following memory
requirements:
P1 200
P2 300
P3 500
Fixed-size partitioning:
If we use fixed-size partitioning with partitions of 250 KB, we can allocate the processes as
follows:
Process Partition
P1 1
P2 2
P3 3 and 4
Variable-size partitioning:
P1 200 KB
P2 300 KB
P3 500 KB
This will eliminate internal fragmentation, but it will require more complex management
algorithms.
Comparison:
#include <iostream>
class Partition {
public:
Partition(int size) {
this->size = size;
this->isAllocated = false;
}
int size;
bool isAllocated;
};
class Process {
public:
Process(int processId, int processSize) {
this->processId = processId;
this->processSize = processSize;
this->partition = nullptr;
}
int processId;
int processSize;
Partition* partition;
};
class MemoryManager {
public:
MemoryManager(Partition* partitions[], int numPartitions) {
this->partitions = partitions;
this->numPartitions = numPartitions;
}
void allocateProcess(Process* process) {
for (int i = 0; i < numPartitions; i++) {
if (!partitions[i]->isAllocated && partitions[i]->size >=
process->processSize) {
partitions[i]->isAllocated = true;
process->partition = partitions[i];
break;
}
}
if (process->partition == nullptr) {
cout << "Insufficient memory to allocate process " <<
process->processId << endl;
}
}
private:
Partition* partitions[];
int numPartitions;
};
int main() {
// Define memory partitions
Partition* partitions[] = {new Partition(200), new Partition(300), new
Partition(150), new Partition(300)};
int numPartitions = sizeof(partitions) / sizeof(Partition*);
// Create processes
Process* process1 = new Process(1, 200);
Process* process2 = new Process(2, 300);
// Allocate processes
memoryManager.allocateProcess(process1);
memoryManager.allocateProcess(process2);
// Deallocate a process
memoryManager.deallocateProcess(process2);
return 0;
}
#include <iostream>
struct Node {
int size;
bool isAllocated;
Node* next;
};
class MemoryManager {
public:
MemoryManager() {
head = nullptr;
}
~MemoryManager() {
Node* currentBlock = head;
while (currentBlock != nullptr) {
Node* nextBlock = currentBlock->next;
delete currentBlock;
currentBlock = nextBlock;
}
}
currentBlock->size = size;
currentBlock->next = newBlock;
}
currentBlock = currentBlock->next;
}
return newBlock;
}
private:
Node* head;
};
int main() {
// Create a memory manager
MemoryManager memoryManager;
return 0;
}