Question
Lab. Assignment #3 Best Fit Algorithm Objectives: -Learn how First Fit algorithm work in memory management. -Learn how to implement First -Fit algorithm in C++.
Lab. Assignment #3
Best Fit Algorithm
Objectives:
-Learn how First Fit algorithm work in memory management.
-Learn how to implement First
-Fit algorithm in C++.
Description:
Write a
C++
program that will implement Best Fit Memory Management A
lgorithm Your program must do the following:
1.Input the memory size and
the number and sizes of all the partitions (limit the max number of the partitions to 5).
2.Input the job list that includes:
-Job's name
-Job's size
3.For each job you should create in your program a
data structure that will include the job
status (Run/Wait) and the partition number (if the job was all
ocated).
4.Calculate and display
initial
memory allocation.
5.Display the memory waste and the jobs that could not be allocated and have to wait
this is what I used for Firt Fit can you just fix it to be best fit, can you plz use .cpp file??
#include
using namespace std;
#define max_partitions 5 // defining max no. of partitions
struct job
{
string j_name;
int j_size;
int partition_no;
int j_sta = 0; // 0 = wait and 1 = Run
};
// Driver code
int main()
{
int memory_size;
cout << "Enter the memory size ";
cin >> memory_size;
int memory_used = 0; // initial memory used
int no_of_partitions; // no. of max_partitions
int no_of_jobs; // for knowing the total no. of jobs
vector
int allocation_no[max_partitions + 1] = { 0 }; // for knowing which jobs are allocated
// 0 = means no job is allocated to any partition
cout << "Enter the no. of partitions ";
cin >> no_of_partitions;
int size_of_partition[max_partitions + 1];
cout << "Enter the size of patitions ";
for (int i = 1; i <= no_of_partitions; i++)
cin >> size_of_partition[i];
cout << "Enter the no. of jobs ";
cin >> no_of_jobs;
job job_data[no_of_jobs + 1]; // creating an array of jobs that are going for algorithm
for (int i = 1; i <= no_of_jobs; i++)
{
cin >> job_data[i].j_name;
cin >> job_data[i].j_size;
int f = 0; // checking whether given job is allocated or not
for (int j = 1; j <= no_of_partitions; j++)
{
if (size_of_partition[j] >= job_data[i].j_size && allocation_no[j] == 0)
{ // checking whether size of partition is greater
// also checking whether this partition is not already allocated
f = 1; // telling it's allocated
allocation_no[j] = i;
job_data[i].partition_no = j;
job_data[i].j_sta = 1; // means process is now in running status
break;
}
}
if (f == 0) // means job is not allocated
notallocatedjobs.push_back(i);
}
// for calculating memory_waste
for (int i = 1; i <= no_of_partitions; i++)
{
if (allocation_no[i] != 0) // means yhis part is unallocated
{
memory_used += size_of_partition[i];
}
}
// now the output part :
cout << " Following are the jobs assigned to parts ";
for (int i = 1; i <= no_of_partitions; i++)
{
int j = allocation_no[i];
if (j == 0)
cout << " For partition no. " << i << " no jobs are alocated ";
else
{
cout << " Partition no. " << i << " is allocated to " << job_data[j].j_name << endl;
}
}
cout << " following are the jobs which are not allocated ";
for (int i = 0; i { cout << job_data[notallocatedjobs[i]].j_name << " "; } cout << " Memory waste : " << memory_size - memory_used << endl; return 0; } Output Screenshot :
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started