Professional Documents
Culture Documents
In This Assignment Banker's Algorithm Is Used
In This Assignment Banker's Algorithm Is Used
Ques. 11. Reena’s operating system uses an algorithm for deadlock avoidance to
manage the
allocation of resources say three namely A, B, and C to three processes P0, P1, and P2.
Consider
the following scenario as reference .user must enter the current state of system as given
in this
example :
Suppose P0 has 0,0,1 instances , P1 is having 3,2,0 instances and P2 occupies 2,1,1
instances of
A,B,C resource respectively.
Also the maximum number of instances required for P0 is 8,4,3 and for p1 is 6,2,0 and
finally for
P2 there are 3,3,3 instances of resources A,B,C respectively. There are 3 instances of
resource A,
2 instances of resource B and 2 instances of resource C available. Write a program to
check
whether Reena’s operating system is in a safe state or not in the following independent
requests
for additional resources in the
current state:
1. Request1: P0 requests 0 instances of A and 0 instances of B and 2 instances of C.
2. Request2: P1 requests for 2 instances of A, 0 instances of B and 0 instances of C.
All the request must be given by user as input.
Description:
Allocation ‘i’ specifies the resources currently allocated to process P ‘i’ and Need
‘i’ specifies the additional resources that process P ‘i’ may still request to complete its task.
Banker’s algorithm consists of Safety algorithm and Resource request algorithm
Safety Algorithm
The algorithm for finding out whether or not a system is in a safe state can be described as
follows:
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k
instances of resource type Rj. When a request for resources is made by process Pi, the
following actions are taken:
1) If Requesti <= Needi
Goto step (2) ; otherwise, raise an error condition, since the process has
exceeded its maximum claim.
2) If Requesti <= Available
Goto step (3); otherwise, Pi must wait, since the resources are not available.
3) Have the system pretend to have allocated the requested resources to
process Pi by modifying the state as
follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi– Requesti
1) Algorithm:
1 #include <stdio.h>
2
3 #include <stdlib.h>
4
5 int main(){
6
7 int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
8
9 int p, r, i, j, process, count;
10
11 count = 0;
12
13
14
printf("Enter the no of processes : ");
15
16 scanf("%d", &p);
17
18
19
20 for(i = 0; i< p; i++)
21
22 completed[i] = 0;
23
24
25
26 printf("\n\nEnter the no of resources : ");
27
scanf("%d", &r);
28
29
30
31 printf("\n\nEnter the Max Matrix for each process : ");
32
33 for(i = 0; i < p; i++)
34
35 {
36
37 printf("\nFor process %d : ", i + 1);
38
for(j = 0; j < r; j++)
39
40
scanf("%d", &Max[i][j]);
41
42 }
43
44
45
46 printf("\n\nEnter the allocation for each process : ");
47
48 for(i = 0; i < p; i++)
49
50 {
51
printf("\nFor process %d : ",i + 1);
52
53
for(j = 0; j < r; j++)
54
55 scanf("%d", &alloc[i][j]);
56
57 }
58
59
60
61 printf("\n\nEnter the Available Resources : ");
62
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
do
printf("\t\t");
printf("\n");
process = -1;
process = i ;
process = -1;
break;
}
}
if(process != -1)
break;
if(process != -1)
safeSequence[count] = process + 1;
count++;
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
if(count == p)
printf(">\n");
else
}
Output
2) MAIN CODE:
#include <assert.h>
#include <memory.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef VERBOSE_ENABLED
#else
#define LOG(...) \
do { \
} while (false);
#endif
/*
* This is a wrapper that will hold all the information about the current system
* state
*/
struct system_state {
int resource_count;
int process_count;
int* avail_resource;
int** allocation_table;
int** max_table;
} * global_system_state, *global_transient_state;
/*
*/
struct request {
int process_id;
int* resource_requests;
} * request_info;
/*
*/
void free_dynamic_resource() {
free(global_system_state->allocation_table[a]);
free(global_system_state->max_table[a]);
free(global_transient_state->max_table[a]);
free(global_transient_state->allocation_table[a]);
free(global_system_state->allocation_table);
free(global_transient_state->allocation_table);
free(global_system_state->max_table);
free(global_transient_state->max_table);
free(global_system_state->avail_resource);
free(global_transient_state->avail_resource);
free(global_system_state);
free(global_transient_state);
/**
* This is function asks for the input of the user and allocates and fills the
* system state.
* */
void input() {
global_system_state =
global_transient_state =
scanf("%d", &(global_system_state->process_count));
global_transient_state->process_count = global_system_state->process_count;
scanf("%d", &(global_system_state->resource_count));
global_transient_state->resource_count = global_system_state->resource_count;
printf(
global_system_state->allocation_table =
(int**)malloc(global_system_state->process_count * sizeof(int*));
global_transient_state->allocation_table =
(int**)malloc(global_transient_state->process_count * sizeof(int*));
global_system_state->allocation_table[a] =
(int*)malloc(global_system_state->resource_count * sizeof(int));
global_transient_state->allocation_table[a] =
(int*)malloc(global_transient_state->resource_count * sizeof(int));
scanf("%d", &(global_system_state->allocation_table[a][b]));
global_transient_state->allocation_table[a][b] =
global_system_state->allocation_table[a][b];
global_system_state->max_table =
(int**)malloc(global_system_state->process_count * sizeof(int*));
global_transient_state->max_table =
(int**)malloc(global_transient_state->process_count * sizeof(int*));
global_system_state->max_table[a] =
(int*)malloc(global_system_state->resource_count * sizeof(int));
global_transient_state->max_table[a] =
(int*)malloc(global_transient_state->resource_count * sizeof(int));
printf(
"\nEnter Maximum Resource Count Limit as Table (Process in rows, and "
scanf("%d", &(global_system_state->max_table[a][b]));
global_transient_state->max_table[a][b] =
global_system_state->max_table[a][b];
global_system_state->avail_resource =
(int*)malloc(sizeof(int) * global_system_state->resource_count);
global_transient_state->avail_resource =
(int*)malloc(sizeof(int) * global_transient_state->resource_count);
printf(
"\nEnter available resource count in the same order as above for each "
"resource : \n");
scanf("%d", &(global_system_state->avail_resource[a]));
global_transient_state->avail_resource[a] =
global_system_state->avail_resource[a];
};
/**
* This is a function responsible for actually finding the stable state if found
* */
/*
These are some useful math functions for easing the solving task
a : available;
b : allocated;
c : required;
*/
return true;
assert(vec_math_is_allocatable(a, b, c, len));
a[iter] += b[iter];
b[iter] = 0;
/*
* a : allocated
* b : requested
* c : max_limit
*/
return true;
/*
* This function restores the state to old state onces a request has been
* processed
*/
void restore() {
global_transient_state->avail_resource[a] =
global_system_state->avail_resource[a];
global_transient_state->allocation_table[a][b] =
global_system_state->allocation_table[a][b];
/*
* This function solves the state after the request has been granted and returns
*/
bool solve() {
bool has_completed[global_system_state->process_count];
has_completed[a] = false;
while (non_executed) {
dead_lock = true;
if (has_completed[a]) continue;
bool res =
vec_math_is_allocatable(global_transient_state->avail_resource,
global_transient_state->allocation_table[a],
global_transient_state->max_table[a],
global_transient_state->resource_count);
if (res) {
has_completed[a] = true;
non_executed--;
dead_lock = false;
vec_math_allocate_and_free(global_transient_state->avail_resource,
global_transient_state->allocation_table[a],
global_transient_state->max_table[a],
global_transient_state->resource_count);
} else {
if (dead_lock) {
LOG("\nEncountered a deadlock...");
return false;
return true;
};
/*
*/
scanf("%d", target);
/*
* This asks the actual request and solves the state and prints if it is safe
*/
void ask_requests(int n) {
int p_c;
scanf("%d", &p_c);
p_c--;
t--;
continue;
}
printf("\nEnter %d space separated integers each for each resource type : ",
global_system_state->resource_count);
request_info->process_id = p_c;
request_info->resource_requests =
(int*)malloc(sizeof(int) * global_system_state->resource_count);
scanf("%d", &(request_info->resource_requests[a]));
if (vec_math_should_grant(global_system_state->allocation_table[p_c],
request_info->resource_requests,
global_system_state->max_table[p_c],
global_system_state->resource_count)) {
if (global_transient_state->avail_resource[a] <
request_info->resource_requests[a]) {
flag = false;
break;
if (flag)
else
printf(
"table");
if (flag)
global_transient_state->avail_resource[a] -=
request_info->resource_requests[a];
global_transient_state->allocation_table[p_c][a] +=
request_info->resource_requests[a];
if (!solve())
printf(
"\nDEADLOCK : After Request was Granted the system went into "
"DEADLOCK");
else
printf(
"\nSystem has a Stable State even after the resource requested was "
"granted");
else {
printf(
free(request_info->resource_requests);
free(request_info);
restore();
printf("\n");
/**
* */
int main() {
input();
if (solve())
else {
return 0;
restore();
int n;
ask_request_count(&n);
ask_requests(n);
free_dynamic_resource();
return 0;
}
3) Constraint Satisfaction
User must enter in this format only
Input format:
Enter allocated resource count as table {process in rows and resource limit in
columns}
Three spaced integers for each resource type must be entered
P0 has 0,0,1 instances , P1 is having 3,2,0 instances and P2 occupies 2,1,1 instances of
A,B,C resource respectively
maximum number of instances required for P0 is 8,4,3 and for p1 is 6,2,0 and finally for
P2 there are 3,3,3 instances of resources A,B,C respectively
4) boundary conditions:
Request 1
P0 can request 0 instance from A, 0 instance from B and only 1 instance from
C.
Request 2:
5) TEST CASE:
Input format:
Sample INPUT:
3
3
001
320
211
843
620
333
322
002
200
Sample OUTPUT
https://github.com/sudhanshu5527/deadlock-assignment