Question

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

15 Apr 2024
Answer :
Word Count : 1112

Banker's Algorithm is a resource allocation and deadlock avoidance algorithm that's used in operating systems to ensure that the allocation of resources doesn't lead to a deadlock situation. It's primarily used in a multi-process and multi-resource environment where processes request a certain number of resources and the system needs to determine whether granting these resources would lead to a safe state, meaning that no deadlock would occur.

Here's a C program implementing the Banker's Algorithm:

```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 n_processes, n_resources;

// Function to check if the requested resources can be granted or not
int is_safe(int process, int request[]) {
    int work[MAX_RESOURCES];
    int finish[MAX_PROCESSES] = {0};
    int i, j;

    // Initialize work to available
    for (i = 0; i < n_resources; ++i)
        work[i] = available[i];

    // Check if request <= need
    for (i = 0; i < n_resources; ++i) {
        if (request[i] > need[process][i])
            return 0;
    }

    // Check if request <= available
    for (i = 0; i < n_resources; ++i) {
        if (request[i] > work[i])
            return 0;
_____ ________ _____ ____ ______ ___ ___ _______ ______ ________.
_________ _______ _______ ___ _______ ________ _______ ____ _________ ______.
__________ __________ ______ ____ ___ ____ ________ ____ _________ ___ ___ ________.
___ __________ _______ ____ ________.
__________ ________ ___ _________ _________ __________.
_____ ___ _____ _____ ____ ___ _________ ______ ____ _____ _______ ___.
___ ____ ________ _________ ____ ___ ________.
______ _______ ______ __________ _________ _____ ______.
___ _______ ________ _________ ____ ________ ___ __________ _________ _____ _______.
__________ __________ ____ ______ _____ ___ _____ ___ _________ ______ ____.
_____ ____ ____ _________ ____ ________ ________ _____.
____ _________ _______ ______ _______ ____ ____ _______ _____.
______ ___ _________ _______ ____ __________ ______.
________ ____ _________ ____ _____ ____ ____ _____ ___ _________ _________.
______ ________ ______ _____ ________ ____ ______.
_________ ____ _____ _____ _______ __________ _________ _____ _____ _______ _______.
_________ ___ ______ ______ __________ __________ ___ ________ _____ ________.
___ _____ ___ ___ ______ ________.
_______ __________ _____ ___ ____ _______ _____ ___ __________ ______ _______.
____ _______ ______ __________ ________ ___ _____ ______ _________ ________.
_______ ______ __________ __________ ___ _______ ________ ____ _______ __________ __________ _____.
_________ ______ _______ _______ _______ ___ __________ _____ _____ ____ ___ ___.
__________ __________ ______ ____ ________ ____ _______ ________ ____.
________ __________ _________ _____ _________ ______ ________ ___ _____ _______.
_______ __________ ____ ________ _____ ________ ___.
__________ ______ ___ ___ _________ ____ _____ __________ _________ ______ ______ __________.
________ ____ ____ _________ _____.
______ _________ ___ ________ _____ _____ _______ _______ ____ ______.
___ _______ ________ _____ ______ ________ ________ ________ ______.
_____ _____ ________ _________ __________ _______ __________ _________ _______.
___ ________ ________ _____ ________ __________ ______ __________.
________ ___ ______ _______ _________.
___ _________ _________ ____ __________ _______ __________ _______.
___ _________ _________ __________ ___ _________ _________ ___ __________ ______ ___ ______.
_____ _______ ____ _____ ________ _____ __________ ______ ________ ____ _______ _________.
___ _________ _______ _______ __________ ___ _____ __________ _________ ____.
_______ _____ ______ __________ ______ _________ ________.
_____ ________ ______ ________ _______ ______ ______ ______ ___.
_______ ____ __________ ____ _________ _____ ________ _____ ______ ______ _____ __________.
_________ ______ _____ ____ _____.
__________ ______ ____ _________ _________ ___ __________ __________ ______.
_____ ________ _________ _____ ________ ___ _________ _________ __________ ___.
_______ _______ _______ _______ _______ ______ ________ _____ _______.
________ _______ __________ _______ __________ __________ _______ _____ ______ ______ _______.
____ _____ ________ _________ ______ ________ ________.
___ ___ __________ _____ _________ _________ _____ _____ ______ __________.
___ ____ _______ ____ ________ _______ ____ _________ ____ __________.
______ ___ _______ ____ _________ _________ ___ _______ _________ ___ __________ ______.
______ _______ _________ _________ ________.
_________ _______ __________ ________ _______ _____ ________ _____.
___ _________ ______ ______ _________ __________ ___ __________ ________ ___.
________ ___ _______ ________ _____ ____ ___ _________ _____.
_________ ________ _________ _______ ______ __________ _________ __________ ____ ___ ____ ________.
________ ________ _________ ______ _________ ____.
____ __________ ______ ___ ______ ____ ___.
______ ______ ____ _______ _______ ___ ____ ________ ___ ____ _______ _____.
______ __________ ______ ________ ________ _______ _____ _____ __________ ___.
______ _____ _______ ___ ________.
____ ____ _________ ____ __________ ________.
_____ _____ ____ ______ _________ ______ ________.
__________ __________ _________ __________ ___.
__________ ___ _________ _____ _______ ________ ______ ___ ______ ____.
___ _______ ______ ______ ____ __________ ______ _______ _______.
___ ________ _________ ______ _____ __________ __________ _________ _____ _________.
_______ ___ ___ ___ ________ __________ ___ _________ ____ ______ _________.
____ ____ _________ _____ __________ __________ __________ _______ __________.
_________ ________ ________ _______ ________.
_______ _________ __________ __________ ____ _________ ____ ________ _____ _________ ____ ______.
_______ ______ ________ ___ _________ ________ _________ ____ _________ ___ _______ ___.
__________ __________ _______ _____ _______.
_______ ____ ____ __________ _________ ________ ___ ________ ______ ___ _______.
_______ __________ ________ ___ ______ ________ _______.
_______ _____ _______ _________ ____.
___ _________ __________ _______ ____ ______ ______ ________ _______ ______.
______ __________ __________ ______ _________ ________.
____ _________ ________ _______ _______ ______ ____ ___ _____ _______ ________ ___.
________ ____ _________ ____ ________ ___ ___ ____ ________ ______ __________.
_________ _______ __________ _________ _____ _______ ________ ______ ________ ________ _________ ____.
___ ____ ___ _______ ____ _________ __________ _____ _________ ____ __________ ___.
___ __________ ___ __________ ______ ___ _________ _________ ____ _______ __________ _______.
____ _____ __________ _______ _______.
__________ ______ _________ ___ ________ ________.
______ _______ ______ _______ __________ _______ ___ ___ __________ ____ ______ ______.
_________ ______ ____ ______ _____ _____.
___ ____ _______ _________ ___ ________.
__________ _________ ______ _________ _________ _____ ___ _________ __________.
_______ __________ ___ ________ __________ __________ __________ ______ _____ _______ ___.
___ ___ ______ ____ _____ __________ ___ ______ _________ ______ ________.
________ ________ ________ _____ ____ ___ _______ __________ __________ ____.
_________ ____ ______ _________ ___.
_______ _____ ________ ______ ____ _________ _________ ____ _______ ______.
__________ ______ ____ ______ ___ ________ _________ ___ ____ _____ ___.
_______ _______ ___ ______ ____ _________ _________ ______ __________ ________.
____ _______ ______ ______ ___ ______.
________ _____ ___ _________ _________ ___ ___.
__________ _______ ________ __________ _______ ________ ___ ______ _______ ___.
_______ _______ ____ _________ _________ _______ ____.

IGNOU ACADEMY
Your IGNOU journey, made easier.
  • Solved Assignments
  • Study Notes
  • Guess Papers
  • Question Papers
Skip in 10
'; fetch( "query.php?type=1&method=0&lr=0&app=401&mycartproduct=1", { method: "GET", cache: "no-store" } ) .then(function(response) { if (!response.ok) { throw new Error( "HTTP " + response.status ); } return response.text(); }) .then(function(html) { loader.innerHTML = html; /* Your existing function, if available. */ if (typeof updateTotals === "function") { updateTotals(); } else { calculateCartTotal(); } }) .catch(function(error) { console.error( "Cart loading error:", error ); loader.innerHTML = '
Unable to load cart.
'; });}/* ========================================================= CALCULATE TOTAL========================================================= */function calculateCartTotal() { let subtotal = 0; /* Looks for .item-price inside AJAX products. Examples accepted: ₹200 ₹200.00 Rs 200 Rs. 200 200 200.00 */ const prices = document.querySelectorAll( '#loadmycart .item-price' ); prices.forEach(function(element) { let text = element.textContent || ''; text = text .replace(/₹/g, '') .replace(/Rs\.?/gi, '') .replace(/,/g, '') .trim(); let value = parseFloat(text); if (!isNaN(value)) { subtotal += value; } }); updateCartDisplay(subtotal);}/* ========================================================= UPDATE TOTAL DISPLAY========================================================= */function updateCartDisplay(total) { const subtotalElement = document.getElementById('subtotal'); const totalElement = document.getElementById('total'); const checkoutButton = document.getElementById('checkoutButton'); const noItems = document.getElementById('noitems'); const amount = Number(total) || 0; if (subtotalElement) { subtotalElement.textContent = '₹' + amount.toFixed(2); } if (totalElement) { totalElement.textContent = '₹' + amount.toFixed(2); } /* EMPTY CART */ if (amount
Top