Lab 5 Develop a C program to simulate Bankers Algorithm for DeadLock Avoidance.

 

Objective:

The objective of this program is to implement the Banker's Algorithm, a deadlock avoidance technique used in operating systems, to ensure safe allocation of resources to processes and prevent deadlock situations.

Overview:

The program simulates the Banker's Algorithm for deadlock avoidance by taking input for the number of processes, the number of resource types available, the maximum resources needed by each process, and the allocated resources for each process.

  1. Input Collection:

    • Collects the number of processes and resources.
    • Takes in available resources.
    • Records allocated resources for each process.
  2. Initialization:

    • Initializes data structures for allocation, maximum, need, and finish arrays.
    • Initializes the need array by calculating the remaining need (maximum - allocated) for each process.
    • Initializes the finish array to track the finish status of each process.
  3. Safety Checking (Banker's Algorithm):

    • Checks if the system is in a safe state by employing the Banker's Algorithm.
    • Simulates the resource allocation by checking if processes can execute safely without causing a deadlock.
    • Determines a safe sequence if one exists and displays it.
  4. Output:

    • Outputs whether the system is in a safe state or an unsafe state.
    • If the system is in a safe state, displays the safe sequence of process execution to avoid deadlock.


#include <stdio.h>


#define MAX_PROCESSES 10

#define MAX_RESOURCES 10


int available[MAX_RESOURCES];

int allocation[MAX_PROCESSES][MAX_RESOURCES];

int need[MAX_PROCESSES][MAX_RESOURCES];

int finish[MAX_PROCESSES];


int num_processes, num_resources;


void input_data() {

    printf("Enter number of processes and resources: ");

    scanf("%d %d", &num_processes, &num_resources);


    printf("Enter available resources: ");

    for (int i = 0; i < num_resources; ++i)

        scanf("%d", &available[i]);


    printf("Enter allocated resources for each process:\n");

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

        for (int j = 0; j < num_resources; ++j) {

            scanf("%d", &allocation[i][j]);

            need[i][j] = allocation[i][j]; // Initialize need array

        }

        finish[i] = 0; // Initialize finish array

    }

}


int is_safe() {

    int work[MAX_RESOURCES];

    for (int i = 0; i < num_resources; ++i)

        work[i] = available[i];


    int safe_sequence[MAX_PROCESSES];

    int count = 0;


    while (count < num_processes) {

        int found = 0;

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

            if (finish[i] == 0) {

                int j;

                for (j = 0; j < num_resources; ++j) {

                    if (need[i][j] > work[j])

                        break;

                }

                if (j == num_resources) {

                    for (int k = 0; k < num_resources; ++k)

                        work[k] += allocation[i][k];

                    safe_sequence[count++] = i;

                    finish[i] = 1;

                    found = 1;

                }

            }

        }

        if (found == 0) {

            printf("System is in an unsafe state!\n");

            return 0;

        }

    }


    printf("System is in a safe state.\nSafe sequence is: ");

    for (int i = 0; i < num_processes - 1; ++i)

        printf("P%d -> ", safe_sequence[i]);

    printf("P%d\n", safe_sequence[num_processes - 1]);

    return 1;

}


int main() {

    input_data();

    is_safe();

    return 0;

}

***********************************************

OUTPUT




Comments