Question

Create a next-fit algorithm in C++. TO THE SOLUTION GIVER, DO NOT POST THE CODE FROM...

Create a next-fit algorithm in C++.

TO THE SOLUTION GIVER, DO NOT POST THE CODE FROM GEEKSFORGEEKS

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include <iostream> // header file for input and output streams
using namespace std; // std class
int main(){// main starts here
int np,nb; // declaring required variables np-> number of processes, nb-> number of memory blocks
cout<<"Enter the number of processes: "; // Data about # processes
cin>>np; // getting the value from stdin
int ps[np][2]; //array for process sizes 2d -> 1 for size and 2nd one to indicate that it is allocated or not
cout<<"Sizes of every process\n"; // Data about size of processes
for(int i=0;i<np;i++) {
cout<<"Enter size of process "<<(i+1)<<": ";
cin>>ps[i][0]; //size of the process taken from stdin
ps[i][1] = 0; //0 means space unallocated
}
cout<<"Enter the number of memory blocks: "; //Data about # memory blocks
cin>>nb; // getting the value from stdin
int mbs[nb][2]; // array for memory blocks sizes 2d -> 1 for size and 2nd one to indicate that it is allocated or not
cout<<"Sizes of every memory block\n"; // Data about size of memory blocks
for(int i=0;i<nb;i++) {
cout<<"Enter size of memory block "<<(i+1)<<": ";
cin>>mbs[i][0]; // size of memory block taken from stdin
mbs[i][1] = 0; //0 means unallocated
}
int j=0;// next fit algorithm starts
for(int i=0;i<np;i++) { // for each process
if(j==nb-1) // to check round of the memory blocks
j=0;
for(;j<nb;j++) // check every memory block
if(mbs[j][1]==0&&ps[i][0]<=mbs[j][0]) { // if memory block unallocated and process size is < memory block size
ps[i][1] = 1; //then that process is allocated to that memory block
mbs[j][1] = 1; //and also change the status of memory block to allocated i.e changing the 2nd dimension from 0 to 1
cout<<"Process id "<<(i+1)<<" allocated to memory block: "<<(j+1)<<endl; // displaying the same
break;
}
}
for(int i=0;i<np;++i) // for unallocated processes
if(ps[i][1]==0) // if process status is unallocated
cout<<"Process id "<<(i+1)<<" is unallocated\n"; // display the same
for(int i=0;i<nb;++i) // for unallocated memory blocks
if(mbs[i][1]==0) // if memory block status is unallocated
cout<<"Memory block "<<(i+1)<<" is free\n"; // display the same
return 0;
}// end of main

OUTPUT:

Below are the 3 images of output in 3 different cases. when number of processes are more than number of memory blocks, when process sizes are more than memory block sizes, next one is fitting every process to every memory block

Add a comment
Know the answer?
Add Answer to:
Create a next-fit algorithm in C++. TO THE SOLUTION GIVER, DO NOT POST THE CODE FROM...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT