Lab. Assignment #2 First Fit Algorithm Objectives: Learn how First-Fit algorithm
ID: 3752349 • Letter: L
Question
Lab. Assignment #2 First Fit Algorithm Objectives: Learn how First-Fit algorithm work in memory management. Learn how to implement First-Fit lgorithm inC Description: Write a C++ program that will implement First-fit, memory management algorithm Your program must do the following: Input the memory size and the number and sizes of all the partitions (limit the max number of the partitions to 5). Input the job list that includes: 1. 2. Job's name Job's size 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 allocated Calculate and display initial memory allocation. Display the memory waste and the jobs that could not be allocated and have to wait. 3. 4. 5. Code Quality Requirement: 1. Do not put every single logic in the main() function. 2. You must use functions to separate your program's logic into smaller readable pieces. 3. You must use descriptive variable names. Don't use int b 5. What's b?! It's okay to use i or j or any other letters for loops (only loops) 4. You must write meaningful comments to explain your code.Explanation / Answer
//C++ program
#include<iostream>
using namespace std;
struct job{
string name;
double size;
};
void Firstfit(struct job job_detail[],int job_count,int allocation[],double memory_block[],int part){
int i,j;
for( i=0;i<job_count;i++){
for( j=0;j<part;i++){
if(memory_block[j]>=job_detail[i].size){
memory_block[j]-=job_detail[i].size;
allocation[i]=j+1;
break;
}
}
if(j==part)allocation[i]=0;
}
}
int main(){
double memory_size;
int partition;
int job_count;
cout<<"Enter memory size : ";
cin>>memory_size;
while(1){
cout<<"Enter number of partitions (1 to 5): ";
cin>>partition;
if(partition>0&&partition<=5)break;
cout<<"Invalid partition choice ";
}
cout<<"Enter number of jobs : ";
cin>>job_count;
struct job job_detail[job_count];
for(int i=0;i<job_count;i++){
cout<<"Enter job name : ";
cin>>job_detail[i].name;
cout<<"Enter job size : ";
cin>>job_detail[i].size;
}
int allocation[job_count];
double memory_block[partition] ;
for(int i=0;i<partition;i++)
memory_block[i]= memory_size/partition;
Firstfit(job_detail,job_count,allocation,memory_block,partition);
for(int i=0;i<job_count;i++){
cout<<job_detail[i].name<<" "<<job_detail[i].size;
if(allocation[i])cout<<" allocate memory block "<<allocation[i]<<" ";
else cout<<"not allocated ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.