0% found this document useful (0 votes)
36 views5 pages

OS LAB Assignment Q: Name - Aditya Reg No - 19BIT0139

The document is an OS lab assignment that simulates first fit, best fit, and worst fit memory allocation algorithms. It contains code that takes in memory block sizes of 250k, 400k, 95k, 500k and process sizes of 260k, 450k, 100k, 650k. The code implements functions for each algorithm that allocate processes to blocks and calculate memory wasted and unallocated processes. The algorithms are run and their outputs are compared to determine the most efficient memory usage.

Uploaded by

aditya
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)
36 views5 pages

OS LAB Assignment Q: Name - Aditya Reg No - 19BIT0139

The document is an OS lab assignment that simulates first fit, best fit, and worst fit memory allocation algorithms. It contains code that takes in memory block sizes of 250k, 400k, 95k, 500k and process sizes of 260k, 450k, 100k, 650k. The code implements functions for each algorithm that allocate processes to blocks and calculate memory wasted and unallocated processes. The algorithms are run and their outputs are compared to determine the most efficient memory usage.

Uploaded by

aditya
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/ 5

Name – Aditya Reg no – 19BIT0139

OS LAB Assignment
Q . You are provided with memory partitions of 250k, 400k, 95k, 500k respectively.
How will you perform first fit, best fit and worst fit algorithms with process of 260k,
450k,100k and 650k(in order). Which algorithm makes the most efficient use of
memory?

Code
#include<bits/stdc++.h>
using namespace std;

// A utility function to print data in a tabular form and reduce code redun
dancy
void printTable(vector<int> block, vector<int>process, vector<int> allocati
on, int n){

// The loop will iterate through the allocation vector and print the bl
ock number stored in it
// It will first check that the process is allocated to any block or no
t
// for this it will compare the ith location of allocation to -1
// if -1 then not allocated else print the block number
pair<int ,int> performance = {0,0};
for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t" << process[i] << "\t\t";
if (allocation[i] != -1){
cout << allocation[i] + 1<<"("<<block[allocation[i]]<<"KB)";
performance.first+=block[allocation[i]]-process[i];
cout<<"\t"<<block[allocation[i]]-process[i]<<endl;
}
else{
cout << "Not Allocated"<<endl;
performance.second++;
}

cout << endl;


}
cout<<"Memory wasted: "<<performance.first<<endl<<"Process not allocate
d: "<<performance.second<<endl<<endl;
}

void firstFit(vector<int> block, int m,vector<int> process, int n)


{
// Allocation vector to store the block number to which the process is
stored
Name – Aditya Reg no – 19BIT0139

// it is initialaised with -
1(as a flag whether or not the process is allocated somewhere or not)
//and of size same as process
vector<int> allocation(n,-1);

// It will find the first block with size greateror equal for teh proce
ss
// and if avaialble then store the index of block to allocation vector
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (block[j] >= process[i])
{
allocation[i] = j;
break;
}
}
}
cout<<"=========================================="<<endl<<"For first fi
t"<<endl<<"=========================================="<<endl;
cout << "\nProcess No.\tProcess Size\tBlock no.\tLoss\n";
printTable(block,process,allocation,n);

void bestFit(vector<int> block, int m,vector<int> process, int n)


{

vector<int> allocation(n,-1);

// this loop iterate through each process and find the best block
// i.e. with least memory loss
// For doing this will choose the smallest block that can fit
// the process among all that is gerater than this process
// store the index of that block to bInd and move on.
for (int i=0; i<n; i++)
{
int bInd = -1;
for (int j=0; j<m; j++){
if (block[j] >= process[i]){
if (bInd== -1)
bInd= j;
else if (block[bInd] > block[j] && allocation[j] ==-1)
bInd = j;
}
}
if (bInd!= -1){
allocation[i] = bInd;
}
}
Name – Aditya Reg no – 19BIT0139

cout<<"=========================================="<<endl<<"For best fit


"<<endl<<"=========================================="<<endl;
cout << "\nProcess No.\tProcess Size\tBlock no.\tLoss\n";
printTable(block,process,allocation,n);

void worstFit(vector<int> block, int m,vector<int> process, int n)


{
vector<int> allocation(n,-1);
vector<int> blockCopy = block;
// This loop will loop through each process and
// find the biggest block for this particular process
// It will store that blocl index and store it to the wstInd
for (int i=0; i<n; i++){
int wstInd = -1;
for (int j=0; j<m; j++){
if (block[j] >= process[i] ){
if (wstInd== -1)
wstInd = j;
else if (block[wstInd] < block[j] && allocation[j] ==-1)
wstInd = j;
}
}
if (wstInd != -1){
allocation[i] = wstInd;
block[wstInd] -= process[i];
}
}
cout<<"=========================================="<<endl<<"For worst fi
t"<<endl<<"=========================================="<<endl;
cout << "\nProcess No.\tProcess Size\tBlock no.\tLoss\n";
pair<int ,int> performance = {0,0};
for (int i = 0; i < n; i++)
{
cout << " " << i+1 << "\t\t" << process[i] << "\t\t";
if (allocation[i] != -1){
cout << allocation[i] + 1<<"("<<blockCopy[allocation[i]]<<"KB)"
;
performance.first+=blockCopy[allocation[i]]-process[i];
cout<<"\t"<<blockCopy[allocation[i]]-process[i]<<endl;
}
else{
cout << "Not Allocated"<<endl;
performance.second++;
}

cout << endl;


}
cout<<"Memory wasted: "<<performance.first<<endl<<"Process not allocate
d: "<<performance.second<<endl<<endl;
Name – Aditya Reg no – 19BIT0139

int main()
{
vector<int> process={260,450,100,650};
vector<int> blocks={250,400,95,500};
int m=4,n=4,temp;
firstFit(blocks,m,process,n);
bestFit(blocks,m,process,n);
worstFit(blocks,m,process,n);
return 0 ;

Output
Name – Aditya Reg no – 19BIT0139

You might also like