Untitled
unknown
plain_text
a year ago
1.4 kB
7
Indexable
#include <iostream> using namespace std; void bestFit(int blockSize[], int processSize[], int m, int n) { // Keep track of allocation for each block int allocation[m] = {-1}; // Traverse through process for (int i = 0; i < n; i++) { int bestIdx = -1; // Index of best fit block // Find the best fit block for current process for (int j = 0; j < m; j++) { if (blockSize[j] >= processSize[i]) { // If not allocated yet or smaller block found if (allocation[j] == -1 || blockSize[j] - processSize[i] < blockSize[bestIdx] - processSize[i]) { bestIdx = j; } } } // If we found a suitable block if (bestIdx != -1) { // Allocate block to process allocation[bestIdx] = i; // Reduce available memory in this block blockSize[bestIdx] -= processSize[i]; cout << "Process No: " << i + 1 << " | Process Size: " << processSize[i] << " | Block No: " << bestIdx + 1 << endl; } else { cout << "Process No: " << i + 1 << " | Process Size: " << processSize[i] << " | Not Allocated" << endl; } } } int main() { int blockSize[] = {100, 50, 30, 120, 35}; int processSize[] = {40, 10, 30, 60}; int m = sizeof(blockSize) / sizeof(blockSize[0]); int n = sizeof(processSize) / sizeof(processSize[0]); cout << "Best Fit Allocation\n"; bestFit(blockSize, processSize, m, n); return 0; }
Editor is loading...
Leave a Comment