Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab. Assignment #2 NF Write a C++-program that will implement Next-Fit memory ma

ID: 3594763 • Letter: L

Question

Lab. Assignment #2 NF Write a C++-program that will implement Next-Fit memory management algorithms. 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 allocated); 4. Calculate and display initial memory allocation. Display the memory waste and the jobs that could not be allocated and have to wait. Output Process No Process Size 212 417 112 426 Block no. Not Allocated NOTE OUTPUT IS FOR BEST-FIT MEMORY ALLOCATION NOT NEXT-FIT BUT THE FORMAT SHOULD LOOK LIKE THIS

Explanation / Answer

#include <bits/stdc++.h>

using namespace std

// This function will allocate memory to blocks as per Next fit algorithm

void NextFit(int blockSize[], int m, int processSize[], int n)

{

// Stores the block id for the block

int allocation[n], j = 0;

// Initially no block

memset(allocation, -1, sizeof(allocation));

// pick each process and find suitable blocks according to its size ad assign to it

for (int i = 0; i < n; i++) {

// Do not start from beginning

while (j < m) {

if (blockSize[j] >= processSize[i]) {

// allocate block j to p[i] process

allocation[i] = j;

// Reduce available memory in this block.

blockSize[j] -= processSize[i];

break;

}

j++;

}

}

cout << " Process No. Process Size Block no. ";

for (int i = 0; i < n; i++) {

cout << " " << i + 1 << " " << processSize[i]

<< " ";

if (allocation[i] != -1)

cout << allocation[i] + 1;

else

cout << "Not Allocated";

cout << endl;

}

}

// Driver program

int main()

{

// Driver program

int main()

{

int blockSize[5];

int processSize[5];

cout<<"Enter blockSize number: ";

cin>>blockSize[5];

cin.ignore();

cout<<"Enter processSize number: ";

cin>>processSize[5];

cin.ignore();

int m = sizeof(blockSize) / sizeof(blockSize[0]);

int n = sizeof(processSize) / sizeof(processSize[0]);

NextFit(blockSize, m, processSize, n);

return 0;

}

}