Question

In: Computer Science

Write a program to detect the deadlock occurrence and write the sequence if there is a...

Write a program to detect the deadlock occurrence and write the sequence if there is a safe state.

Noted: C or C++

Solutions

Expert Solution

#include <iostream>
using namespace std;

int main()
{
   // P0, P1, P2, P3, P4 are the Process names here

   int n, m, i, j, k;
   n = 5; // Number of processes
   m = 3; // Number of resources
   int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix
                       { 2, 0, 0 }, // P1
                       { 3, 0, 2 }, // P2
                       { 2, 1, 1 }, // P3
                       { 0, 0, 2 } }; // P4

   int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix
                   { 3, 2, 2 }, // P1
                   { 9, 0, 2 }, // P2
                   { 2, 2, 2 }, // P3
                   { 4, 3, 3 } }; // P4

   int avail[3] = { 3, 3, 2 }; // Available Resources

   int f[n], ans[n], ind = 0;
   for (k = 0; k < n; k++) {
       f[k] = 0;
   }
   int need[n][m];
   for (i = 0; i < n; i++) {
       for (j = 0; j < m; j++)
           need[i][j] = max[i][j] - alloc[i][j];
   }
   int y = 0;
   for (k = 0; k < 5; k++) {
       for (i = 0; i < n; i++) {
           if (f[i] == 0) {

               int flag = 0;
               for (j = 0; j < m; j++) {
                   if (need[i][j] > avail[j]){
                       flag = 1;
                       break;
                   }
               }

               if (flag == 0) {
                   ans[ind++] = i;
                   for (y = 0; y < m; y++)
                       avail[y] += alloc[i][y];
                   f[i] = 1;
               }
           }
       }
   }

   cout << "Following is the SAFE Sequence" << endl;
   for (i = 0; i < n - 1; i++)
       cout << " P" << ans[i] << " ->";
   cout << " P" << ans[n - 1] <<endl;

   return (0);
}

if you have any doubts ask me..


Related Solutions

write the vhdl code to get the moore and mealy machine to detect the sequence 1101...
write the vhdl code to get the moore and mealy machine to detect the sequence 1101 (in c++)
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
1. Design a sequence detector, a Mealy finite state machine to detect the serial bit sequence...
1. Design a sequence detector, a Mealy finite state machine to detect the serial bit sequence 1101, where the most significant bit (MSB) comes first and the least significant bit comes last. A) Draw the state diagram B) Draw the state table C) The circuit is to be implemented using JK flip-flops and combinational logic circuit. Derive the Boolean expression necessary for this implementation. D) Sketch the circuit diagram for your design. This should show all the flipflops, logic gates...
Write a program sequence that will test the byte STATUS and branch to ROOTINE_1 if bit...
Write a program sequence that will test the byte STATUS and branch to ROOTINE_1 if bit 1, 2 or 4 is 1. Otherwise, it is branch to ROUTINE_2 if both bits 1 and 3 are 1 and to ROUTINE_3 if both bits 1 and 3 are 0. In all other cases it is to exeute ROUTINE_4. Assume that the routines are more than 128 bytes long. Also give a flowchart of the sequence.
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
Input the available amount of Rice in kg. Write a program that reads a sequence of...
Input the available amount of Rice in kg. Write a program that reads a sequence of Rice donations in kg and add them to the initially available amount of Rice. The loop should stop once the accumulated amount of Rice exceed 50 kg. Then, the program should print the final amount of Rice and the number of considered donations.
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Design a state machine to recognize if a binary string contains any occurrence of the sequence...
Design a state machine to recognize if a binary string contains any occurrence of the sequence “10101”. Is it possible to design this state machine with less states? By using Finite State machine designer. http://madebyevan.com/fsm/
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence...
( Assembly Language ) Write a program that computes the 7th fibonacci number. The fibonacci sequence - 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, … what is the initialization of a, b, and d? - a b d 1 ? ? 1 2 ? ? 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 7 5 8 13 wrong initialization - a b d 1 0 1 1 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT