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

Bscs4a Lab

The document describes fixed-size and variable-size partitioning algorithms for allocating processes to memory, where fixed-size partitioning can lead to internal fragmentation but is simpler while variable-size eliminates fragmentation but is more complex; it provides an example of allocating three processes of sizes 200KB, 300KB, and 500KB to a system with 1000KB of memory using each algorithm.

Uploaded by

Amina Khan
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)
9 views7 pages

Bscs4a Lab

The document describes fixed-size and variable-size partitioning algorithms for allocating processes to memory, where fixed-size partitioning can lead to internal fragmentation but is simpler while variable-size eliminates fragmentation but is more complex; it provides an example of allocating three processes of sizes 200KB, 300KB, and 500KB to a system with 1000KB of memory using each algorithm.

Uploaded by

Amina Khan
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

Operating System (Lab Manual)

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:

Process Memory requirement (KB)

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

This will leave 100 KB of memory unallocated, which is internal fragmentation.

Variable-size partitioning:

If we use variable-size partitioning, we can allocate the processes as follows:

Process Memory block

P1 200 KB

P2 300 KB

P3 500 KB

This will eliminate internal fragmentation, but it will require more complex management
algorithms.

Comparison:

Fixed-size partitioning is simpler to implement and manage than variable-size partitioning.


However, it can lead to internal fragmentation, which can waste memory. Variable-size
partitioning eliminates internal fragmentation, but it is more complex to implement and
manage.

Fixed Size Allocation

#include <iostream>

using namespace std;

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;
}
}

void deallocateProcess(Process* process) {


if (process->partition != nullptr) {
process->partition->isAllocated = false;
process->partition = nullptr;
}
}

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 a memory manager


MemoryManager memoryManager(partitions, numPartitions);

// 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);

// Delete the partitions


for (int i = 0; i < numPartitions; i++) {
delete partitions[i];
}

// Delete the processes


delete process1;
delete process2;

return 0;
}

Variable Size Partitioning

#include <iostream>

using namespace std;

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;
}
}

void* allocate(int size) {


// Check if there is a free block of the required size
Node* currentBlock = head;
while (currentBlock != nullptr) {
if (!currentBlock->isAllocated && currentBlock->size >= size) {
// Split the block if necessary
if (currentBlock->size > size) {
Node* newBlock = new Node;
newBlock->size = currentBlock->size - size;
newBlock->isAllocated = false;
newBlock->next = currentBlock->next;

currentBlock->size = size;
currentBlock->next = newBlock;
}

// Mark the block as allocated


currentBlock->isAllocated = true;

// Return the block to the caller


return currentBlock;
}

currentBlock = currentBlock->next;
}

// Allocate a new block if there is no free block of the required size


Node* newBlock = new Node;
newBlock->size = size;
newBlock->isAllocated = true;
newBlock->next = nullptr;

// Add the block to the head of the free list


newBlock->next = head;
head = newBlock;

return newBlock;
}

void deallocate(void* pointer) {


Node* block = (Node*)pointer;

// Mark the block as free


block->isAllocated = false;
// Move the block to the head of the free list
block->next = head;
head = block;
}

private:
Node* head;
};

int main() {
// Create a memory manager
MemoryManager memoryManager;

// Allocate a memory block of 100 bytes


void* pointer = memoryManager.allocate(100);

// Use the memory block


// ...

// Deallocate the memory block


memoryManager.deallocate(pointer);

return 0;
}

You might also like