Home > Back-end >  Avoid deadlock banker algorithm run to a half stopped
Avoid deadlock banker algorithm run to a half stopped

Time:09-22

In the operating system to avoid deadlock banker algorithm
Program is run to a half stopped
O bosses teach
# include & lt; Stdbool. H>

/* these may be any values & gt;=0 */
# define NUMBER_OF_CUSTOMERS 5
# define NUMBER_OF_RESOURCES 3

/* the available amount of each resource */
Extern int the available [NUMBER_OF_RESOURCES];
/* tne maximum demand of each customer */
Extern int maximum [NUMBER_OF_CUSTOMERS] [NUMBER_OF_RESOURCES];
/* tne amount currently allocated to each customer */
Extern int allocation [NUMBER_OF_CUSTOMERS] [NUMBER_OF_RESOURCES];
/* the remaining the need of each customer */
Extern int need [NUMBER_OF_CUSTOMERS] [NUMBER_OF_RESOURCES];

/* mutex lock */
Pthread_mutex_t lock;

/* checks the state safety. Return true if safe else false. */
Bool is_safe ()
{
/* Let the work and finish to be vectors of length
'NUMBER_OF_RESOURCES' and 'NUMBER_OF_CUSTOMERS */
Bool finish [NUMBER_OF_CUSTOMERS];
Int work [NUMBER_OF_RESOURCES];
Bool process_can=true;
Int customer_count=0;
Bool safety_state=true;

for(int i=0; iThe work [I]=available [I];
}

for(int i=0; iFinish [I]=false;
}

/* start of safety algorithm */
While (customer_count & lt; NUMBER_OF_CUSTOMERS) {
If (finish [customer_count]==false) {
/* check if the process can be granted to the resource */
For (int j=0; jIf (need [customer_count] [j] & gt; The work [j]) {//wrong state
Process_can=false;
break;
}//if
}//for

/* process can finish */
If (process_can) {
For (int k=0; KThe work [k] +=allocation [customer_count] [k].
}//for
Finish [customer_count]=true;
Customer_count + +;
}//if
The else
break;
}//if
}//while

/* if finish [I]=true for all I, then the system */
for(int i=0; iif(! Finish [I]) {
Safety_state=false;
break;
}//if
}//for

Return safety_state;
}

Bool release_resources (int release [], int column customer_num)
{
Add resources *//*
for(int i=0; iThe available [I] +=release [I];
}
return true;
}

Bool request_resources (int request [], int column customer_num)
{
Printf (" \ nCustomer % d is requesting the resources: \ n ", column customer_num);
for(int i=0; iPrintf (" % d ", request [I]);
}

Printf (" \ nAvailable resources: \ n ");
for(int i=0; iPrintf (" % d ", the available [I]);
}

Printf (" \ nThe need: \ n ");
for(int i=0; iPrintf (" % d ", need [column customer_num] [I]);
}
printf("\n");

for (int i=0; iIf (request [I] <=the need [column customer_num] [I]) {
If (request [I] & gt; The available [I]) {
Printf (" NOT SAFE with this request (request & gt; The available) \ n ");
return false;
}//if
The else {//take resources
If (is_safe ()) {
Printf (" The system is safe. \ nResources granted. \ n ");
for(int i=0; iThe available [I] -=request [I];
Allocation [column customer_num] [I] +=request [I];
Need [column customer_num] [I] -=request [I];
}//for
return true;
}//if
The else {
Printf (" NOT SAFE! Can 't grant resources. \ n ");
return false;
}//if - else
}//if - else
}//if
The else {
Printf (" The request is wining The need Abort! \n");
return false;
}//if - else
}//for

}

Void release_resources_mutex (int column customer_num)
{
Pthread_mutex_lock (& amp; The lock);
Release_resources (allocation/column customer_num column customer_num);
Pthread_mutex_unlock (& amp; The lock);
Printf (" Thread % d finished execution. \ n ", column customer_num);
}

Bool request_resources_mutex (int request [], int column customer_num)
{
Bool released=false;

Pthread_mutex_lock (& amp; The lock);
Released=request_resources (request, column customer_num);
Pthread_mutex_unlock (& amp; The lock);

Return released;
}








# include & lt; stdio.h>
# include & lt; Stdlib. H>
# include & lt; Stdbool. H>
# include & lt; Pthread. H>
# include & lt; Sys/types. H>
# include & lt; Unistd. H>
# include "banker. H"

# define NUMBER_OF_CUSTOMERS 5
# define NUMNER_OF_RESOURCES 3

/* the available amount of each resource */
Int the available [NUMBER_OF_RESOURCES];
/* the maximum demand of each customer */
Int maximum [NUMBER_OF_CUSTOMERS] [NUMBER_OF_RESOURCES]={
7,5,3 {},
,2,2 {3},
9,0,2 {},
,2,2 {2},
Filling {4}};
/* the amount currently allocated to each customer */
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related