Join Whatsapp Channel for Ignou latest updates JOIN NOW

Write a program in C to implement Banker’s Algorithm to avoid Deadlock. Also explain the code briefly

Sure, here’s a simple implementation of the Banker’s Algorithm in C:

#include <stdio.h>

#define MAX_PROCESSES 10
#define MAX_RESOURCES 10

int available[MAX_RESOURCES];
int maximum[MAX_PROCESSES][MAX_RESOURCES];
int allocation[MAX_PROCESSES][MAX_RESOURCES];
int need[MAX_PROCESSES][MAX_RESOURCES];
int num_processes, num_resources;

// Function to check if the current state is safe or not
int isSafe(int process_seq[], int finish[]) {
    int work[MAX_RESOURCES];
    int i, j;

    // Initialize work array with available resources
    for (i = 0; i < num_resources; ++i)
        work[i] = available[i];

    // Initialize finish array
    for (i = 0; i < num_processes; ++i)
        finish[i] = 0;

    // Loop until all processes are finished or deadlock is detected
    int count = 0;
    while (count < num_processes) {
        int found = 0;
        for (i = 0; i < num_processes; ++i) {
            if (!finish[i]) {
                int canExecute = 1;
                for (j = 0; j < num_resources; ++j) {
                    if (need[i][j] > work[j]) {
                        canExecute = 0;
                        break;
                    }
                }
                if (canExecute) {
                    // Process can be executed
                    for (j = 0; j < num_resources; ++j)
                        work[j] += allocation[i][j];
                    finish[i] = 1;
                    process_seq[count++] = i;
                    found = 1;
                }
            }
        }
        // If no process can be executed, deadlock detected
        if (!found)
            return 0;
    }
    return 1; // If all processes are finished without deadlock
}

// Function to simulate resource allocation
void allocateResources(int process_id, int request[]) {
    int i;
    for (i = 0; i < num_resources; ++i) {
        available[i] -= request[i];
        allocation[process_id][i] += request[i];
        need[process_id][i] -= request[i];
    }
}

// Function to release resources after process execution
void releaseResources(int process_id) {
    int i;
    for (i = 0; i < num_resources; ++i) {
        available[i] += allocation[process_id][i];
        allocation[process_id][i] = 0;
        need[process_id][i] = 0;
    }
}

int main() {
    int i, j;

    printf("Enter the number of processes: ");
    scanf("%d", &num_processes);

    printf("Enter the number of resources: ");
    scanf("%d", &num_resources);

    printf("Enter the available resources: ");
    for (i = 0; i < num_resources; ++i)
        scanf("%d", &available[i]);

    printf("Enter maximum resources required by each process: \n");
    for (i = 0; i < num_processes; ++i) {
        printf("Process %d: ", i);
        for (j = 0; j < num_resources; ++j)
            scanf("%d", &maximum[i][j]);
    }

    printf("Enter resources currently allocated to each process: \n");
    for (i = 0; i < num_processes; ++i) {
        printf("Process %d: ", i);
        for (j = 0; j < num_resources; ++j) {
            scanf("%d", &allocation[i][j]);
            need[i][j] = maximum[i][j] - allocation[i][j];
        }
    }

    // Perform resource allocation
    int process_seq[MAX_PROCESSES];
    int finish[MAX_PROCESSES];
    if (isSafe(process_seq, finish)) {
        printf("Safe sequence: ");
        for (i = 0; i < num_processes; ++i)
            printf("%d ", process_seq[i]);
        printf("\n");
    } else {
        printf("Deadlock detected!\n");
    }

    return 0;
}

Explanation:

  1. Data Structures: The program defines arrays to store available, maximum, allocation, and need resources for processes.
  2. isSafe(): This function checks whether the current state is safe or not by simulating the resource allocation and checking if all processes can be executed without deadlock.
  3. allocateResources(): This function allocates resources to a process and updates the available, allocation, and need arrays accordingly.
  4. releaseResources(): This function releases resources after a process is executed and updates the available, allocation, and need arrays accordingly.
  5. Main Function: It takes input for the number of processes, number of resources, available resources, maximum resources required by each process, and resources currently allocated to each process. Then, it calls isSafe() to check for a safe sequence and prints the sequence if it exists, otherwise, it detects deadlock.
error: Content is protected !!