Question

In: Computer Science

write a C/C++ program that implements the banker's algorithm. Verify your implementation using the data from...

write a C/C++ program that implements the banker's algorithm. Verify your implementation using the data from the textbook as well as the attached, but unverified (meaning that it is possible the system is already in an unsafe state), file. The file format is as follows:

Line 1 contains a number of resources (m).

Line 2 contains the quantity for each resource (I.e., the resource vector.

Line 3 contains the number of processes (n).

Lines 4 through 3+n contain the Claim matrix. Each line contains the maximum resource requirements for one process.

Lines 4+n and beyond contain the Allocation matrix. Each line contains the current number of resources allocated to the process.

The Claim and Allocation of matrices are in the same process order.

Solutions

Expert Solution

Prerequisite: Banker’s Algorithm

The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation for predetermined maximum possible amounts of all resources, then makes an “s-state” check to test for possible activities, before deciding whether allocation should be allowed to continue.

Following Data structures are used to implement the Banker’s Algorithm:

Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.

Available:

  • It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
  • Available[ j ] = k means there are ‘k’ instances of resource type Rj

Max :

  • It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system.
  • Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.

Allocation :

  • It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently allocated to each process.
  • Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj

Need :

  • It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
  • Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj
  • Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]

Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional resources that process Pi may still request to complete its task.

Banker’s algorithm consist of Safety algorithm and Resource request algorithm

Safety Algorithm

The algorithm for finding out whether or not a system is in a safe state can be described as follows:

  1. Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
    Initialize: Work= Available
    Finish [i]=false; for i=1,2,……,n
  2. Find an i such that both a) Finish [i]=false b) Need_i<=work

  3. if no such i exists goto step (4)   Work=Work + Allocation_i   Finish[i]= true goto step(2)
  4. If Finish[i]=true for all i,
    then the system is in safe state.     Safe sequence is the sequence in which the processes can be safely executed.

// C++ program to illustrate Banker's Algorithm

#include<iostream>

using namespace std;

// Number of processes

const int P = 5;

// Number of resources

const int R = 3;

// Function to find the need of each process

void calculateNeed(int need[P][R], int maxm[P][R],

                   int allot[P][R])

{

    // Calculating Need of each P

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

        for (int j = 0 ; j < R ; j++)

            // Need of instance = maxm instance -

            //                    allocated instance

            need[i][j] = maxm[i][j] - allot[i][j];

}

// Function to find the system is in safe state or not

bool isSafe(int processes[], int avail[], int maxm[][R],

            int allot[][R])

{

    int need[P][R];

    // Function to calculate need matrix

    calculateNeed(need, maxm, allot);

    // Mark all processes as infinish

    bool finish[P] = {0};

    // To store safe sequence

    int safeSeq[P];

    // Make a copy of available resources

    int work[R];

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

        work[i] = avail[i];

    // While all processes are not finished

    // or system is not in safe state.

    int count = 0;

    while (count < P)

    {

        // Find a process which is not finish and

        // whose needs can be satisfied with current

        // work[] resources.

        bool found = false;

        for (int p = 0; p < P; p++)

        {

            // First check if a process is finished,

            // if no, go for next condition

            if (finish[p] == 0)

            {

                // Check if for all resources of

                // current P need is less

                // than work

                int j;

                for (j = 0; j < R; j++)

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

                        break;

                // If all needs of p were satisfied.

                if (j == R)

                {

                    // Add the allocated resources of

                    // current P to the available/work

                    // resources i.e.free the resources

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

                        work[k] += allot[p][k];

                    // Add this process to safe sequence.

                    safeSeq[count++] = p;

                    // Mark this p as finished

                    finish[p] = 1;

                    found = true;

                }

            }

        }

        // If we could not find a next process in safe

        // sequence.

        if (found == false)

        {

            cout << "System is not in safe state";

            return false;

        }

    }

    // If system is in safe state then

    // safe sequence will be as below

    cout << "System is in safe state.\nSafe"

         " sequence is: ";

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

        cout << safeSeq[i] << " ";

    return true;

}

// Driver code

int main()

{

    int processes[] = {0, 1, 2, 3, 4};

    // Available instances of resources

    int avail[] = {3, 3, 2};

    // Maximum R that can be allocated

    // to processes

    int maxm[][R] = {{7, 5, 3},

                     {3, 2, 2},

                     {9, 0, 2},

                     {2, 2, 2},

                     {4, 3, 3}};

    // Resources allocated to processes

    int allot[][R] = {{0, 1, 0},

                      {2, 0, 0},

                      {3, 0, 2},

                      {2, 1, 1},

                      {0, 0, 2}};

    // Check system is in safe state or not

    isSafe(processes, avail, maxm, allot);

    return 0;

}


Related Solutions

please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
Write a program that implements the A* algorithm to find a path from any two given...
Write a program that implements the A* algorithm to find a path from any two given nodes. You may use any of the following languages: C++, C#, Java, ActionScript. Problem Overview & Algorithm Description In a fully-observable environment where there are both pathable and blocked nodes, an agent must find a good path from their starting node to the goal node. The agent must use the A* algorithm to determine its path. For this program, you must use the Manhattan...
Create a program called DualPivotQuickSort.java that implements the QuickSort algorithm using Yaroslavskiy’s algorithm. The program should...
Create a program called DualPivotQuickSort.java that implements the QuickSort algorithm using Yaroslavskiy’s algorithm. The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file...
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
Write an MPI program that implements the hypercube bitonic sort algorithm. The program should be tested...
Write an MPI program that implements the hypercube bitonic sort algorithm. The program should be tested using a sequence of 64 integers (from 1 to 64, initially not sorted) and 8 processes. The number of elements assigned to each process is 8.
Using C or C++, write a program which implements the command pipeline ”ls -la | tr...
Using C or C++, write a program which implements the command pipeline ”ls -la | tr [a-zA-Z0-9] a”. Use any combination of fork, exec, wait, read, and write necessary to create this functionality. Note: the parent process must be the one to output the data to the console.
Given a queue of integers, write an algorithm and the program in c++ that, using only...
Given a queue of integers, write an algorithm and the program in c++ that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
Please find implementation of KNN algorithm (in C++) below Please explain how each code implements the...
Please find implementation of KNN algorithm (in C++) below Please explain how each code implements the following steps of the algorithm in question: 1. Determine parameter K = number of nearest neighbors 2. Calculate the distance between the query-instance and all the training samples 3. Sort the distance and determine nearest neighbors based on the K-th minimumdistance 4. Gather the category Y of the nearest neighbors 5. Use simple majority of the category of nearest neighbors as the prediction value...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT