Green University of Bangladesh
Department of Computer Science and Engineering (CSE)
Faculty of Sciences and Engineering
Semester: (Spring, Year:2024), [Link]. in CSE (Day)
Lab Report NO # 05
Course Title: Operating System Lab
Course Code: CSE 310 Section: D28
Lab Experiment Name: Implement first fit contiguous memory allocation
algorithm.
Student Details
Name ID
1. Nur Hasan Hasib 221902247
Lab Date : 01/04/24
Submission Date : 20/04/24
Course Teacher’s Name : Sheikh Fazle Rabbi
Lab Report Status
Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................
1. TITLE OF THE LAB REPORT EXPERIMENT
Implement first fit contiguous memory allocation algorithm.
2. OBJECTIVES
• Implement a memory allocation algorithm using the first fit strategy.
• Initialize and input memory blocks and file sizes.
• Allocate each file to the first available memory block that can accommodate it.
• Track and display the allocation status, including fragmentation and unused blocks.
• Ensure the program handles cases where files cannot be allocated due to insufficient block sizes.
• Present the results in a clear and organized tabular format.
3. PROCEDURE
Algorithm:
1. Initialize Arrays: Set arrays b, f, frag, bf, and ff to 0.
2. Input Number of Blocks and Files: Read nb (number of blocks), Read nf (number of files).
3. Input Block Sizes: For each block, read its size into array b.
4. Input File Sizes: For each file, read its size into array f.
5. First Fit Allocation: For each file i , For each block j , If block j is free (bf[j] == 0) and can
accommodate file i (b[j] >= f[i]),Allocate file i to block j. Calculate and store fragmentation.
Mark block j as allocated and lastly Break inner loop.
6. Output Results: For each file, print its allocation details or indicate if not allocated.
4. IMPLEMENTATION & OUTPUT
First, I create the make directory using this command(mkdir) in linux terminal.
And then Enter the directory and create a file using (nano) command. Give the permission using
(chmod) for execution. And for the Output use (gcc -o) command.
Source C code:
#include <stdio.h>
#define max 25
void main() {
int frag[max], b[max], f[max], i, j, nb, nf, temp;
int bf[max], ff[max];
for (i = 0; i < max; i++) {
b[i] = 0;
f[i] = 0;
frag[i] = 0;
bf[i] = 0;
ff[i] = 0;
}
printf("\n input the number of blocks: ");
scanf("%d", &nb);
printf("input the number of files: ");
scanf("%d", &nf);
printf("\ninput the size of the blocks:\n");
for (i = 1; i <= nb; i++) {
printf("Block %d: ", i);
scanf("%d", &b[i]);
}
printf("input the size of the files:\n");
for (i = 1; i <= nf; i++) {
printf("File %d: ", i);
scanf("%d", &f[i]);
}
for (i = 1; i <= nf; i++) {
for (j = 1; j <= nb; j++) {
if (bf[j] == 0 && b[j] >= f[i]) {
ff[i] = j;
frag[i] = b[j] - f[i];
bf[j] = 1;
break;
}
}
}
printf("\nFile_no \tFile_size \tBlock_no \tBlock_size \tFragment");
for (i = 1; i <= nf; i++) {
if (ff[i] != 0) {
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, f[i], ff[i], b[ff[i]], frag[i]);
} else {
printf("\n%d\t\t%d\t\tNot Allocated", i, f[i]);
}
}
}
Here, is the ultimate result of this problem:
We can see here, file no and their size and block no and block size and fragment.
Inside the nano text Editor:
5. ANALYSIS AND DISCUSSION
In discussion, this problem addresses the implementation of the first fit contiguous memory
allocation algorithm in C, which is used in operating systems for managing memory. The
algorithm involves allocating each file to the first available block of memory that is large
enough to accommodate it. This approach ensures simplicity and efficiency in memory
allocation by sequentially searching for the first suitable block, minimizing the search time.
The program also calculates and displays the resulting fragmentation, providing insight
into memory utilization and wasted space. The implementation involves initializing data
structures, taking user inputs for block and file sizes, performing the allocation, and finally
presenting the allocation results in a clear format.