Question

In: Computer Science

Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5,...

Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5, number of resources 3 and the number of instances of each given resource is in available. You should complete the functionalities for safe state check and resource request processing.

To Do

1. Complete the definition of isSafe function. The function take, the process array, 1D array of available resources, 2D array storing current allocation, and 2D array of current need. The function does not return any value. If the system is not in safe state, the function prints “System is not in safe state”, else the function prints “System is in safe state. Safe sequence is:” with the safe sequence printed subsequently.

2. Complete the definition of processReq function. The function does not return any value. The function takes the process id, corresponding 1D array specifying resource request, 1D array of available resources, 2D array storing current allocation, and 2D array of current need.

The incomplete code:

#include<iostream>
using namespace std;
// Number of processes
const int numP = 5;
// Number of resources
const int numR = 3;
// Max resource allocation to each process
int maxAlloc[][numR] = {{ 7, 5, 3 }, { 3, 2, 2 }, { 9, 0, 2 }, { 2, 2,2 }, { 4, 3, 3 }};
// Calculate the resource need for a process
void calcNeed(int need[][numR], int allot[][numR])
{
// Calculating Need of each Process
for (int i = 0; i < numP; i++)
for (int j = 0; j < numR; j++)
// Need of instance = maxm instance - allocated instance
need[i][j] = maxAlloc[i][j] - allot[i][j];
}
// Function to find the system is in safe state or not
void isSafe(int processes[], int avail[], int allot[][numR], int need[][numR])
{
// Place Your Code here
}
// Function to check if given resource request can be processed.
void processReq(int proID, int req[], int avail[], int allot[][numR], int need[][numR])
{
// Place Your Code here
}
// Main Function
int main()
{
int processes[] = {0, 1, 2, 3, 4};

// Resources allocated to processes
int allot[][numR] = {{0, 1, 0},{2, 0, 0},{3, 0, 2},{2, 1, 1},{0, 0, 2}};
// Resource Need
int procNeed[numP][numR];
// Available instances of resources
int avail[] = {3, 3, 2};
cout << "System information at time - T0" << endl;
cout << "Maximum allocation to different processes " << endl;
for(int i = 0; i < numP; i++)
{
for(int j = 0; j < numR; j++)
cout << maxAlloc[i][j] << " ";
cout << endl;
}

cout << "Available resouces instances at time - T0" << endl;
for(int j = 0; j < numR; j++)
cout << avail[j] << " ";
cout << endl;
cout << "Allocation to different processes at - T0 " << endl;
for(int i = 0; i < numP; i++)
{
for(int j = 0; j < numR; j++)
cout << allot[i][j] << " ";
cout << endl;
}
calcNeed(procNeed, allot);
// Check system is in safe state or not
isSafe(processes, avail, allot, procNeed);
return 0;
}

Solutions

Expert Solution

void processReq(int proID, int req[], int avail[], int allot[][numR], int need[][numR])
{

bool flag_request=true;

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

{

if (request[j] > need[process_request][j])

{

flag_request=false;

break;

}

if (request[j] > available[j])

{

flag_request=false;

break;

}

}

if (flag_request == false)

cout<<"\nTHE REQUEST CAN NOT BE GRANTED .... X "<<endl;

else

{

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

{

available[j]-=request[j];

allocation[process_request][j]+=request[j];

need[process_request][j]-=request[j];

}

bool safe_request= safe( allocation ,need ,available );

if (safe_request == true)

{

cout<<"\nTHE REQUEST CAN BE GRANTED ...."<<endl;

          

cout<<"\nThe Available Vector is..."<<endl;

cout<<" ";

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

cout<<' '<< r[j] ;

cout<<"\n ";

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

{

cout<<work[j] <<' ';}

cout<<endl;

}

else

cout<<"\nTHE REQUEST CAN NOT BE GRANTED BECAUSE \nTHE SYSTEM IS NOT IN A SAFE STATE! ...."<<endl;

      

}

}

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

            int allot[][R], int need[][R])

{

    // 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;

}


Related Solutions

Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes...
Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes and M resource types (N<10,M<10). Use C/C++/C# or Java for the implementation, with a simple text interface, where the user enters only the name of the input file (text only). The program reads all the necessary input data from that file. The input data and result is then displayed on the screen. You may use your program to validate the example you gave in...
Consider the bankers algorithm for deadlock avoidance. Give an example of this algorithm for 7 processes...
Consider the bankers algorithm for deadlock avoidance. Give an example of this algorithm for 7 processes and 5 resource types.
Deadlock –Banker’s Algorithm A system has three resource types (A, B, C) and four processes {P1,...
Deadlock –Banker’s Algorithm A system has three resource types (A, B, C) and four processes {P1, P2, P3, P4 }. The total units of system resources are: (8, 5, 4) units of A, B and C, respectively. The maximum demands for each process is P1(1,2,3), P2(3,2,1), P3(6,5,4) and P4(4,4,2). The current allocation is: P1(0,1,1), P2(2,2,0) and P3(3,0,1) and P4(1,0,1). (a) Allocation table is given for the 3 processes with the following four columns: PROCESS, ALLOCATION, MAX and NEED. And fill...
1. How numerically ordered resources method prevent deadlock using example. 1. How banker’s algorithm works on...
1. How numerically ordered resources method prevent deadlock using example. 1. How banker’s algorithm works on deadlock prevention using example
Deadlocks. The Banker's algorithm is used for deadlock avoidance. Consider the state of resource availability and allocation defined by the following matrices.
Deadlocks. The Banker's algorithm is used for deadlock avoidance. Consider the state of resource availability and allocation defined by the following matrices.(1) Assuming that the total amounts for resources R1, R2, and R3 are 10, 2, and 10, should a new request to the Banker's algorithm by process P3 to acquire one additional resource from R1 and one additional resource from R3 be approved or denied? Explain why or why not(2) Assuming that the total amounts for resources R1, R2,...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page faults and page hits by considering the Frame size=4, ReferenceString:2 4 6 7 8 2 4 9 13 9 2 7 2 6 1 4 9 2
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.
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
Write a program to implement the Romberg Algorithm. Input: f(x), an interval [a,b], N (the number...
Write a program to implement the Romberg Algorithm. Input: f(x), an interval [a,b], N (the number of subintervals on the finest grid on level 0 is 2^N, therefore, N is usualy a small integer) Output: the triangle generated by Romberg Algorithm.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT