[go: up one dir, main page]

0% found this document useful (0 votes)
16 views3 pages

Prac 10

The document presents a C program that implements the Banker’s Algorithm for deadlock avoidance, simulating a system with 5 processes and 3 resource types. It includes functions to check if the system is in a safe state and to handle resource requests while ensuring that the system remains safe. Sample output demonstrates the initial state and the granting of a resource request for Process 1, confirming the system's safe state.

Uploaded by

moccasincaitlin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Prac 10

The document presents a C program that implements the Banker’s Algorithm for deadlock avoidance, simulating a system with 5 processes and 3 resource types. It includes functions to check if the system is in a safe state and to handle resource requests while ensuring that the system remains safe. Sample output demonstrates the initial state and the granting of a resource request for Process 1, confirming the system's safe state.

Uploaded by

moccasincaitlin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Implement the Banker’s Algorithm for Deadlock

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

61 // Check if request is valid


62 for ( int i = 0; i < M ; i ++) {
63 if ( request [ i ] > need [ pid ][ i ]) {
64 printf ( " Error : Process % d has exceeded its maximum claim .\ n
" , pid ) ;
65 return false ;
66 }
67 if ( request [ i ] > avail [ i ]) {
68 printf ( " Resources not available for Process % d .\ n " , pid ) ;
69 return false ;
70 }
71 }
72
73 // Temporarily allocate resources
74 int tempAvail [ M ];
75 int tempAllot [ N ][ M ];
76 for ( int i = 0; i < M ; i ++) {
77 tempAvail [ i ] = avail [ i ] - request [ i ];
78 tempAllot [ pid ][ i ] = allot [ pid ][ i ] + request [ i ];
79 }
80
81 // Check if the system remains in a safe state
82 if ( isSafe (( int []) {0 , 1 , 2 , 3 , 4} , tempAvail , max , tempAllot ) ) {
83 // Update allocation and available resources
84 for ( int i = 0; i < M ; i ++) {
85 avail [ i ] = tempAvail [ i ];
86 allot [ pid ][ i ] = tempAllot [ pid ][ i ];
87 }
88 printf ( " Request granted for Process % d .\ n " , pid ) ;
89 return true ;
90 } else {

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

You might also like