To explain the banker algorithm, we must first explain the safe state and unsafe state of the operating system.
Security status: If there are security sequences P 1, …, Pn composed of all processes in the system, the system is in a security status. The security state must be no deadlock.
Unsafe status: Security sequence does not exist. Unsafe state does not necessarily lead to deadlock.
So what is a secure sequence?
Secure sequence: The process sequence is secure. If for each process Pi( 1≤i≤n), the amount of resources it still needs in the future will not exceed the current remaining resources PJ (j) of the system and all processes.
Banker algorithm:
We can think of the operating system as a banker. The resources managed by the operating system are equivalent to the funds managed by the banker, and the process's request for allocating resources to the operating system is equivalent to the user's loan to the banker. The operating system allocates resources to processes according to the rules set by bankers. When a process applies for resources for the first time, it should test the maximum demand for resources. If the existing resources of the system can meet its maximum demand, the resources should be allocated according to the current application volume, otherwise the allocation will be postponed. When the process continues to apply for resources during the execution, it is first tested whether the sum of the resources occupied by the process and the resources applied for this time exceeds the maximum demand for resources by the process. If it is exceeded, it will refuse to allocate resources; If not, it will test whether the existing resources in the system can meet the maximum amount of resources still needed in the process; If possible, resources will be allocated according to the current application volume; Otherwise it will be postponed.
Algorithm:
N: the total number of processes in the system
M: the total number of resource classes
Available: integers of array [1..m];
Max: an integer of array [1.. n, 1..m];
Allocation: integer of array [1..n, 1..m];
Expected: integer of array [1..n, 1..m];
Request: integer of array [1..n, 1..m];
Symbol description:
Available remaining resources
Maximum maximum demand
Allocate allocated resources
Need demand resources
Request for resources
When the process pi applies for resources, the system performs the following operations.
Step: ("=" is the assignment symbol, "= =" is the equal sign)
If requested, perform step (1)
Step (2) If required,
Step (3) Assuming that the system allocates resources, there are:
Available = available-request;
Allocation = allocation+request;
Need = need-request
If the new state of the system is safe, the allocation is complete.
If the new state of the system is unsafe, the original state is restored and the process waits.
For security check, define the data structure:
Work: Integer of array [1..m];
Completion: array [1 ... boolean n];
Steps of safety inspection:
Step (1):
Work = available;
Finish = false
Step (2) Find me:
a.Finish = = false
B. Need < = work;
If not, go to step (4)
Third step
Work = work+assignment;
Finish = true
Go to step (2)
Step (4) If the finish of all I's is true, the system is in a safe state, otherwise it is in an unsafe state.
/* Banker Algorithm, OS Concept 6th Edition.
Edited by Johnny hagen of SCAU, running in vc6.0.
*/
# contains "malloc.h"
# contains "stdio.h"
# contains "stdlib.h"
# define alloy len sizeof (structure allocation)
#define maxlen sizeof(struct max)
# Define avalen sizeof (Structure Available)
# Define needlen sizeof (structural need)
# Define finilen sizeof (end of structure)
#define pathlen sizeof (structure path)
Structural distribution
{
Int value;
Structure allocation * next
};
Maximum structure
{
Int value;
struct max * next
};
Available structure/* Number of available resources */
{
Int value;
Structure available * next
};
Structural requirements/* Number of resources required */
{
Int value;
Structure needs * next.
};
Structural path
{
Int value;
Structure path * next
};
Structural completion
{
int stat
struct finish * next
};
int main()
{
int row,colum,status=0,I,j,t,temp,processtest
Structure allocation * allochhead, *alloc 1, *alloc2, * alloctemp.
struct max *maxhead,*maxium 1,*maxium2,* maxtemp
struct available *avahead,*available 1,*available2,*workhead,*work 1,*work2,*worktemp,* work temp 1;
struct need *needhead,*need 1,*need2,* needtemp
struct finish *finihead,*finish 1,*finish2,* finishtemp
Structure path * path header, * path 1, * path 2;
Printf ("\ nPlease enter the number of system resource types:");
scanf("%d ",& ampcolum);
Printf ("Please enter the number of processes currently in memory:");
Scanf("%d ",& platoon);
Printf ("Please enter the matrix of allocated resources: \ n");
for(I = 0; I< platoon; i++)
{
for(j = 0; j & ltcolumj++)
{
Printf ("Please enter %c system resources allocated to process p%d:", i,' a'+j);
If (status ==0)
{
allochead = alloc 1 = alloc 2 =(struct allocation *)malloc(alloclen);
alloc 1->next = alloc 2-& gt; next = NULL
scanf("%d ",& ampallochead-& gt; Value);
status++;
}
other
{
alloc 2 =(struct allocation *)malloc(alloclen);
Scanf("%d, %d ",& alloc2-> value);
If (status == 1)
{
allochead-& gt; next = alloc2
status++;
}
alloc 1->next = alloc2
alloc 1 = alloc 2;
}
}
}
alloc2->next = NULL
Status = 0;
Printf ("Please enter the maximum demand matrix: \ n");
for(I = 0; I< platoon; i++)
{
for(j = 0; j & ltcolumj++)
{
Printf ("Please enter the maximum system resource requirement of process p%d category %c:", i,' a'+j);
If (status ==0)
{
maxhead = maxium 1 = maxium 2 =(struct max *)malloc(maxlen);
maxium 1->; next = maxium 2-& gt; next = NULL
scanf("%d ",& ampmaxium 1->; Value);
status++;
}
other
{
maxium 2 =(struct max *)malloc(maxlen);
scanf("%d,%d ",& ampmaxium 2->; Value);
If (status == 1)
{
maxhead-& gt; next = maxium2
status++;
}
maxium 1->; next = maxium2
maxium 1 = maxium 2;
}
}
}
maxium 2->; next = NULL
Status = 0;
Printf ("Please enter the resource matrix remaining in the current system: \ n");
for(j = 0; j & ltcolumj++)
{
Printf ("Remaining system resources of category %c:",' a'+j);
If (status ==0)
{
ava head = available 1 = available 2 =(struct available *)malloc(avalen);
work head = work 1 = work 2 =(struct available *)malloc(avalen);
available 1->; next = available 2-& gt; next = NULL
work 1->; next = work 2-& gt; next = NULL
scanf("%d ",& ampavailable 1->; Value);
work 1->; value = available 1-& gt; Value;
status++;
}
other
{
available 2 =(struct available *)malloc(avalen);
work 2 =(struct available *)malloc(avalen);
Scanf("%d, %d ",&2- > values are available);
Task 2->; value = available 2-& gt; Value;
If (status == 1)
{
ava head-& gt; next = available2
Work head->; next = work2
status++;
}
available 1->; next = available2
available 1 = available 2;
work 1->; next = work2
work 1 = work 2;
}
}
Available 2->next = NULL
Task 2->; next = NULL
Status = 0;
alloctemp = allochead
maxtemp = maxhead
for(I = 0; I< platoon; i++)
for(j = 0; j & ltcolumj++)
{
If (status ==0)
{
need head = need 1 = need 2 =(struct need *)malloc(needlen);
need 1->; next = need 2-& gt; next = NULL
need 1->; value = maxtemp-& gt; value-allocatemp-& gt; Value;
status++;
}
other
{
need 2 =(struct need *)malloc(needlen);
need 2->; value =(maxtemp-& gt; value)-(alloct EMP-& gt; Value);
If (status == 1)
{
needhead-& gt; next = need2
status++;
}
need 1->; next = need2
need 1 = need 2;
}
max temp = max temp-& gt; Next;
alloctemp = alloctemp-& gt; Next;
}
need 2->; next = NULL
Status = 0;
for(I = 0; I< platoon; i++)
{
If (status ==0)
{
fini head = finish 1 = finish 2 =(struct finish *)malloc(finilen);
finish 1->; next = finish 2-& gt; next = NULL
finish 1->; stat = 0;
status++;
}
other
{
finish 2 =(struct finish *)malloc(finilen);
Complete 2-> stat = 0;
If (status == 1)
{
fini head-& gt; next = finish2
status++;
}
finish 1->; next = finish2
finish 1 = finish 2;
}
}
Complete 2->next = NULL/* Initialization complete */
Status = 0;
process test = 0;
for(temp = 0; Temp & lt row; temp++)
{
alloctemp = allochead
needtemp = needhead
finishtemp = finihead
worktemp = workhead
for(I = 0; I< platoon; i++)
{
Operating temperature 1 = operating temperature;
if(finish temp-& gt; stat==0)
{
for(j = 0; j & ltcolumj++,need temp = need temp-& gt; Next, working temperature = working temperature->; Next)
if(need temp-& gt; Value < = operating temperature->; Value)
process test++;
if(processtest==colum)
{
for(j = 0; j & ltcolumj++)
{
worktemp 1->value+= alloct EMP-& gt; Value;
Operating temperature 1 = operating temperature1->; Next;
alloctemp = alloctemp-& gt; Next;
}
If (status ==0)
{
path head = path 1 = path 2 =(struct path *)malloc(path len);
path 1->; next = path 2-& gt; next = NULL
path 1->; value = I;
status++;
}
other
{
path 2 =(struct path *)malloc(path len);
path2->value = I;
If (status == 1)
{
path head->; next = path2
status++;
}
path 1->; next = path2
path 1 = path 2;
}
finish temp-& gt; stat = 1;
}
other
{
for(t = 0; t & ltcolumt++)
alloctemp = alloctemp-& gt; Next;
finish temp-& gt; stat = 0;
}
}
other
for(t = 0; t & ltcolumt++)
{
need temp = need temp-& gt; Next;
alloctemp = alloctemp-& gt; Next;
}
process test = 0;
worktemp = workhead
finish temp = finish temp-& gt; Next;
}
}
path2->next = NULL
finishtemp = finihead
for(temp = 0; Temp & lt row; temp++)
{
if(finish temp-& gt; stat==0)
{
Printf ("\ nThe system is in an unsafe state! \ n ");
Exit (0);
}
finish temp = finish temp-& gt; Next;
}
Printf ("\ nThe system is in a safe state. \ n ");
Printf ("\ nsecurity sequence is: \ n");
do
{
printf("p%d ",path head-& gt; Value);
}
while(path head = path head-& gt; Next);
printf(" \ n ");
Returns 0;
}
In addition, there are group-buying products on the stationmaster group, which are cheap and guaranteed.