0% found this document useful (0 votes)
4 views9 pages

Memory Allocation Algorithms Guide

The document provides a practical manual for implementing memory allocation algorithms (First Fit, Best Fit, Worst Fit) and disk management operations. It includes detailed explanations of each algorithm, sample C programs for implementation, and concepts related to disk management such as partitioning and formatting. Additionally, it emphasizes the importance of attendance in understanding the material covered in sessions.
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)
4 views9 pages

Memory Allocation Algorithms Guide

The document provides a practical manual for implementing memory allocation algorithms (First Fit, Best Fit, Worst Fit) and disk management operations. It includes detailed explanations of each algorithm, sample C programs for implementation, and concepts related to disk management such as partitioning and formatting. Additionally, it emphasizes the importance of attendance in understanding the material covered in sessions.
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

Practical Manual: Memory Allocation Algorithms (First Fit, Best Fit,

Worst Fit) & Disk Management

1. Objective
To study and implement various memory allocation algorithms such as First Fit, Best Fit, and
Worst Fit. Additionally, to understand basic disk management operations.

2. Theory
2.1 Memory Allocation
Memory allocation in Operating Systems is the process of assigning blocks of memory to
programs and processes. Different allocation strategies define how the OS chooses the block of
memory from the available free spaces.

2.2 Types of Allocation Algorithms


• First Fit: The allocator assigns the first block that is large enough.

• Best Fit: The allocator assigns the smallest block that is sufficient.

• Worst Fit: The allocator assigns the largest available block.

3. Algorithms
3.1 First Fit Algorithm
1. Start from the first memory block.
2. If the block is free and large enough, allocate it.
3. Otherwise, continue to the next block.

3.2 Best Fit Algorithm


1. Unlike First Fit, which always begins searching from the starting block, Next Fit continues its
search from the last allocated block.
2. This helps reduce search time in large memory systems.
3. If memory blocks are arranged in a ring-like manner, the search wraps around to block 0
when it reaches the end.

3.3 Best Fit Algorithm


1. Traverse all memory blocks.
2. Select the block with minimum size that is still large enough.
3. Allocate the selected block.

3.4 Worst Fit Algorithm


1. Traverse all memory blocks.
2. Select the block with maximum size.
3. Allocate the selected block.
4. Sample C Programs

3.1 First Fit Algorithm

#include <stdio.h>

void firstFit(int blockSize[], int m, int processSize[], int n) {


int allocation[n];
for (int i = 0; i < n; i++) allocation[i] = -1;

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


for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
allocation[i] = j;
blockSize[j] -= processSize[i];
break;
}
}
}

printf("\nProcess No.\tProcess Size\tBlock No.\n");


for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d", allocation[i] + 1);
else
printf("Not Allocated");
printf("\n");
}
}
int main() {
int blockSize[] = {100, 500, 200, 300, 600};
int processSize[] = {212, 417, 112, 426};
int m = 5, n = 4;
firstFit(blockSize, m, processSize, n);
return 0;
}
3.2 Next Fit Algorithm

#include <stdio.h>

void nextFit(int blockSize[], int m, int processSize[], int n) {

int allocation[n];

for (int i = 0; i < n; i++) allocation[i] = -1;

int j = 0; // Start position for next search

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

int count = 0; // To avoid infinite loop

while (count < m) {

if (blockSize[j] >= processSize[i]) {

allocation[i] = j;

blockSize[j] -= processSize[i];

break;

j = (j + 1) % m;

count++;

printf("\nProcess No.\tProcess Size\tBlock No.\n");

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

printf("%d\t\t%d\t\t", i + 1, processSize[i]);

if (allocation[i] != -1)

printf("%d", allocation[i] + 1);


else

printf("Not Allocated");

printf("\n");

int main() {

int blockSize[] = {100, 500, 200, 300, 600};

int processSize[] = {212, 417, 112, 426};

int m = 5, n = 4;

nextFit(blockSize, m, processSize, n);

return 0;

}
3.3 Best Fit Algorithm

#include <stdio.h>

void bestFit(int blockSize[], int m, int processSize[], int n) {

int allocation[n];

for (int i = 0; i < n; i++) allocation[i] = -1;

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

int bestIdx = -1;

for (int j = 0; j < m; j++) {

if (blockSize[j] >= processSize[i]) {

if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx])

bestIdx = j;

if (bestIdx != -1) {

allocation[i] = bestIdx;

blockSize[bestIdx] -= processSize[i];

printf("\nProcess No.\tProcess Size\tBlock No.\n");

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

printf("%d\t\t%d\t\t", i + 1, processSize[i]);

if (allocation[i] != -1)

printf("%d", allocation[i] + 1);


else

printf("Not Allocated");

printf("\n");

int main() {

int blockSize[] = {100, 500, 200, 300, 600};

int processSize[] = {212, 417, 112, 426};

int m = 5, n = 4;

bestFit(blockSize, m, processSize, n);

return 0;

}
3.4 Worst Fit Algorithm

#include <stdio.h>

void worstFit(int blockSize[], int m, int processSize[], int n) {

int allocation[n];

for (int i = 0; i < n; i++) allocation[i] = -1;

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

int worstIdx = -1;

for (int j = 0; j < m; j++) {

if (blockSize[j] >= processSize[i]) {

if (worstIdx == -1 || blockSize[j] > blockSize[worstIdx])

worstIdx = j;

if (worstIdx != -1) {

allocation[i] = worstIdx;

blockSize[worstIdx] -= processSize[i];

printf("\nProcess No.\tProcess Size\tBlock No.\n");

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

printf("%d\t\t%d\t\t", i + 1, processSize[i]);

if (allocation[i] != -1)
printf("%d", allocation[i] + 1);

else

printf("Not Allocated");

printf("\n");

int main() {

int blockSize[] = {100, 500, 200, 300, 600};

int processSize[] = {212, 417, 112, 426};

int m = 5, n = 4;

worstFit(blockSize, m, processSize, n);

return 0;

}
5. Disk Management Concepts
Disk management involves partitioning, formatting, and maintaining storage devices.
Some topics to perform in the lab:
• Creating partitions
• Formatting disks (FAT32, NTFS, EXT4)
• Viewing disk performance
• Understanding fragmentation (SSD does not require defragmentation)

The Disk Management concepts were covered in depth during Friday’s session
(21-11-25). Those who were absent have unfortunately missed important explanations and
practical insights. Please note that attending sessions is your own responsibility, and missing
them may affect your understanding and performance.
Kindly ensure regular attendance to avoid such gaps in the future.

6. Expected Output

NOTE FOR BATCH A ,B & C

(IMPLEMENT ALL ALGORITHMS , DISK MANAGEMENT CONCEPT AND


COMPARE THE RESULTS)

You might also like