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++)
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
c program Write a program that asks the user to enter a sequence of 15 integers,...
c program Write a program that asks the user to enter a sequence of 15 integers, each either being 0, 1, or 2, and then prints the number of times the user has entered a "2" immediately following a "1". Arrays are not allowed to appear in your code. Include ONLY THE SCREENSHOT OF YOUR CODE in an image file and submit the file.
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...
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,
WRITE A FIBONACCI SEQUENCE AS A RECURSIVE FUNCTION IN MIPS ASSEMBLY LANGUAGE WRITE A TEST PROGRAM...
WRITE A FIBONACCI SEQUENCE AS A RECURSIVE FUNCTION IN MIPS ASSEMBLY LANGUAGE WRITE A TEST PROGRAM (“MAIN” PROGRAM) THAT CALLS IT AT LEAST TWICE CONFORM TO ALL CODING STANDARDS
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/
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT