Prac 10
Prac 10
Avoidance
Program
The following C program implements the Banker’s Algorithm for deadlock avoidance. It
simulates a system with 5 processes and 3 resource types, checking if granting a resource
request leads to a safe state. The program includes a function to check the safety of the
system and processes a sample resource request to demonstrate deadlock avoidance.
1 # include < stdio .h >
2 # include < stdbool .h >
3
4 # define N 5 // Number of processes
5 # define M 3 // Number of resource types
6
7 // Function to check if the system is in a safe state
8 bool isSafe ( int processes [] , int avail [] , int max [][ M ] , int allot [][ M ])
{
9 int need [ N ][ M ];
10 // Calculate the Need matrix
11 for ( int i = 0; i < N ; i ++)
12 for ( int j = 0; j < M ; j ++)
13 need [ i ][ j ] = max [ i ][ j ] - allot [ i ][ j ];
14
15 // Initialize finish array and safe sequence
16 bool finish [ N ] = {0};
17 int safeSeq [ N ];
18 int work [ M ];
19 for ( int i = 0; i < M ; i ++)
20 work [ i ] = avail [ i ];
21
22 int count = 0;
23 while ( count < N ) {
24 bool found = false ;
25 for ( int p = 0; p < N ; p ++) {
26 if (! finish [ p ]) {
27 int j ;
28 for ( j = 0; j < M ; j ++)
29 if ( need [ p ][ j ] > work [ j ])
30 break ;
31 if ( j == M ) {
32 for ( int k = 0; k < M ; k ++)
33 work [ k ] += allot [ p ][ k ];
34 safeSeq [ count ++] = p ;
35 finish [ p ] = true ;
1
36 found = true ;
37 }
38 }
39 }
40 if (! found ) {
41 printf ( " System is not in a safe state .\ n " ) ;
42 return false ;
43 }
44 }
45
46 printf ( " System is in a safe state .\ nSafe sequence is : " ) ;
47 for ( int i = 0; i < N ; i ++)
48 printf ( " P % d " , safeSeq [ i ]) ;
49 printf ( " \ n " ) ;
50 return true ;
51 }
52
53 // Function to check if a resource request can be granted
54 bool requestResources ( int pid , int request [] , int avail [] , int max [][ M
] , int allot [][ M ]) {
55 int need [ N ][ M ];
56 // Calculate the Need matrix
57 for ( int i = 0; i < N ; i ++)
58 for ( int j = 0; j < M ; j ++)
59 need [ i ][ j ] = max [ i ][ j ] - allot [ i ][ j ];
60
2
91 printf ( " Request denied for Process % d to avoid deadlock .\ n " ,
pid ) ;
92 return false ;
93 }
94 }
95
96 int main () {
97 int processes [] = {0 , 1 , 2 , 3 , 4};
98 // Allocation Matrix
99 int allot [][ M ] = {
100 {0 , 1 , 0} ,
101 {2 , 0 , 0} ,
102 {3 , 0 , 2} ,
103 {2 , 1 , 1} ,
104 {0 , 0 , 2}
105 };
106 // Max Matrix
107 int max [][ M ] = {
108 {7 , 5 , 3} ,
109 {3 , 2 , 2} ,
110 {9 , 0 , 2} ,
111 {2 , 2 , 2} ,
112 {4 , 3 , 3}
113 };
114 // Available Resources
115 int avail [] = {3 , 3 , 2};
116
117 // Check initial system state
118 printf ( " Initial system state :\ n " ) ;
119 isSafe ( processes , avail , max , allot ) ;
120
121 // Sample resource request by Process 1
122 int request [] = {1 , 0 , 2};
123 printf ( " \ nProcess 1 requests resources : [1 , 0 , 2]\ n " ) ;
124 requestResources (1 , request , avail , max , allot ) ;
125
126 return 0;
127 }
Listing 1: C Program for Banker’s Algorithm for Deadlock Avoidance
Output
The output for the given input (Allocation Matrix, Max Matrix, Available Resources,
and a sample request by Process 1) is as follows:
1 Initial system state :
2 System is in a safe state .
3 Safe sequence is : P1 P3 P4 P0 P2
4
5 Process 1 requests resources : [1 , 0 , 2]
6 System is in a safe state .
7 Safe sequence is : P1 P3 P4 P0 P2
8 Request granted for Process 1.
Listing 2: Sample Output