Banker's Algorithm Program In C++ User Input

7 min read Jun 28, 2024
Banker's Algorithm Program In C++ User Input

Banker's Algorithm Implementation in C++ with User Input

This article will guide you through implementing the Banker's Algorithm in C++ with user input. The Banker's Algorithm is a resource allocation and deadlock avoidance algorithm used in operating systems. It ensures that a system can safely allocate resources to processes while preventing deadlocks.

Understanding the Banker's Algorithm

The Banker's Algorithm operates based on the following principles:

  1. Resource Allocation: The system maintains a fixed number of resources, which are allocated to processes based on their need and the available resources.
  2. Safety Check: The algorithm checks whether a state is safe, meaning that it can allocate resources to processes without causing a deadlock.
  3. Deadlock Prevention: The algorithm ensures that only safe states are reached, preventing any potential deadlocks.

C++ Implementation with User Input

Here's a C++ implementation of the Banker's Algorithm, including user input for defining the system's resources and process requests:

#include 
#include 

using namespace std;

// Function to check if the system is in a safe state
bool isSafe(vector> &available, vector> &allocation, vector> &max, int n, int m) {
    vector finish(n, false);
    vector safeSequence;
    vector work(available); // Copy available resources to work vector

    while (safeSequence.size() < n) {
        bool found = false;
        for (int i = 0; i < n; i++) {
            if (!finish[i] && 
                all_of(max[i].begin(), max[i].end(), 
                        { return work[max_need] >= max_need - allocation[i][max_need]; })) {
                for (int j = 0; j < m; j++) {
                    work[j] += allocation[i][j];
                }
                finish[i] = true;
                safeSequence.push_back(i);
                found = true;
                break;
            }
        }
        if (!found) {
            return false; // System is not in a safe state
        }
    }

    cout << "Safe Sequence: ";
    for (int i : safeSequence) {
        cout << "P" << i << " ";
    }
    cout << endl;

    return true;
}

int main() {
    int n, m;

    cout << "Enter the number of processes: ";
    cin >> n;
    cout << "Enter the number of resources: ";
    cin >> m;

    // Input for available resources
    vector available(m);
    cout << "Enter the available resources: ";
    for (int i = 0; i < m; i++) {
        cin >> available[i];
    }

    // Input for maximum resource requirements for each process
    vector> max(n, vector(m));
    cout << "Enter the maximum resource requirements for each process:\n";
    for (int i = 0; i < n; i++) {
        cout << "Process P" << i << ": ";
        for (int j = 0; j < m; j++) {
            cin >> max[i][j];
        }
    }

    // Input for allocated resources for each process
    vector> allocation(n, vector(m));
    cout << "Enter the allocated resources for each process:\n";
    for (int i = 0; i < n; i++) {
        cout << "Process P" << i << ": ";
        for (int j = 0; j < m; j++) {
            cin >> allocation[i][j];
        }
    }

    // Check for safe state
    if (isSafe(available, allocation, max, n, m)) {
        cout << "The system is in a safe state." << endl;
    } else {
        cout << "The system is not in a safe state." << endl;
    }

    return 0;
}

Explanation:

  1. Headers and Namespace: Include necessary headers and use the std namespace.
  2. isSafe Function:
    • Takes the available resources, allocated resources, maximum needs of processes, number of processes, and number of resources as inputs.
    • Initializes a finish vector to track whether a process has completed execution, a safeSequence vector to store the safe order of processes, and a work vector to track available resources.
    • Iterates through processes until all are marked finished.
    • For each process:
      • If the process is not yet finished and its maximum needs are less than or equal to available resources, the process is marked as finished, and its allocated resources are added back to work.
    • If the loop completes without all processes being marked finished, the system is not in a safe state.
    • Otherwise, the safeSequence vector contains the safe execution order of processes.
  3. main Function:
    • Takes user input for the number of processes, resources, available resources, maximum needs, and allocated resources.
    • Calls the isSafe function to check the system's state.
    • Prints the results indicating whether the system is in a safe state and, if so, the safe sequence of processes.

Running the Program

To use the program, compile and run it. You'll be prompted to enter the system's configuration (number of processes, resources, etc.). The program will then determine if the system is in a safe state and output the safe sequence if it is.

This C++ implementation of the Banker's Algorithm provides a clear and structured approach to handling user input and executing the algorithm to ensure safe resource allocation and prevent deadlocks.

Featured Posts