Untitled

 avatar
unknown
plain_text
a year ago
1.5 kB
5
Indexable
#include <iostream>

using namespace std;

void nextFit(int blockSize[], int m, int processSize[], int n) {
    int allocation[n];
    for (int i = 0; i < n; i++)
        allocation[i] = -1;

    int lastIndex = 0;

    for (int i = 0; i < n; i++) {
        for (int j = lastIndex; j < m; j++) {
            if (blockSize[j] >= processSize[i]) {
                allocation[i] = j;
                blockSize[j] -= processSize[i];
                lastIndex = j;
                break;
            }
        }

        if (allocation[i] == -1) {
            for (int j = 0; j < lastIndex; j++) {
                if (blockSize[j] >= processSize[i]) {
                    allocation[i] = j;
                    blockSize[j] -= processSize[i];
                    lastIndex = j;
                    break;
                }
            }
        }
    }

    cout << "Process No.\tProcess Size\tBlock No." << endl;
    for (int i = 0; i < n; i++) {
        cout << " " << i + 1 << "\t\t" << processSize[i] << "\t\t";
        if (allocation[i] != -1)
            cout << allocation[i] + 1;
        else
            cout << "Not Allocated";
        cout << endl;
    }
}

int main() {
    int blockSize[] = {5, 10, 20};
    int processSize[] = {10, 20, 30};
    int m = sizeof(blockSize) / sizeof(blockSize[0]);
    int n = sizeof(processSize) / sizeof(processSize[0]);

    cout << "Next Fit Memory Allocation" << endl;
    nextFit(blockSize, m, processSize, n);

    return 0;
}
Editor is loading...
Leave a Comment