First of all, the following code refers to this Daniel.
However, the code directly given by Daniel, where the algorithm is optimized is no better explanation, here I analyzed a lot of time.
Program is mainly process simulation and algorithm implementation, relatively easy to understand.
Then there is a description of the Banker's algorithm optimization: (paste directly on his own experimental report)
Security algorithm: After satisfying the relationship between available and need, the security check is satisfied for the request assignment. Resources are allocated after satisfying the relationship between request and need and the relationship between request and available. At the same time, the process states of readiness, wait, completion, etc. are added, all defined in the struct.
Stochastic algorithms: As with security algorithms, the only difference is that security checks are replaced with available>0.
Improved algorithm evaluation:
Common Security Algorithms:
(1), submit the request;
(2), the use of the remaining available distribution, if the allocation is successful, then safe, output security sequence; otherwise unsafe, rollback, allocation failure.
Improved security algorithms:
Known: Once a resource can be used less than a process, if the request to implement the process, not only the process can not be completed (the allocation of resources to achieve the purpose, in essence, a waste of resources, no existing resources), the consumption of resources to a large extent limit the execution of other processes, Thus entering an unsafe state and deadlock. If a security check is made on the process before a resource request is made, available does not meet need and then skips the process and waits so that some unnecessary requests can be avoided.
Advantages: (1), avoid some meaningless requests, make the limit scope of the request more clear, real life increased efficiency;
(2), compared to the original banker algorithm cancels the resource request after a series of security checks (must use for to see if all resources are completed, in fact, also dynamic allocation, time-consuming), greatly reducing time-consuming.
Disadvantage: If a portion of the requested resources does not affect the execution of other processes, the improved algorithm will block, which will actually make some of the original valid requests become unenforceable.
Summary: Banker algorithms both in reality and in terms of avoiding deadlocks, the goal is to see if a security sequence can be found, so that all processes (users) meet the requirements. The disadvantage of the sub-optimization does not affect the results of the security sequence, but only a very small amount of meaningless operation, in exchange for the algorithm performance improvement and time reduction, so I think the sub-optimization is feasible.
Some places in the program may also change according to their own tastes, but the basic ideas are the Daniel's.
PS: Curriculum Design This thing to remember the report to write well, I report XJB wrote a report results are medium, the total score is dragged to a good =
Code:
#include <iostream> #include <stdio.h> #include <stdlib.h> #define OS ten using namespace std;
struct REC {int A;
int B;
int C;
}; struct PCB {int Id; Process number, external identifier char state; Status R Ready W wait E to complete REC Apply; Current request volume REC Max; Total demand REC need; Also requires a number of resources REC Allocation;
Allocated amount};
PCB *pos,*tmp, *pos0;
REC System, Available;
PCB Pcb[3];
/*void INITPCB () {printf ("Total input system resources: \ n");
scanf ("%d%d%d", &system.a, &system.b, &SYSTEM.C);
int i = 0, a = 0, b = 0, c = 0;
for (pos = PCB; pos < pcb+3; pos++) {i++;
Pos->id = i;
pos->state = ' R ';
Pos->allocation.a = 0;
pos->allocation.b = 0;
POS->ALLOCATION.C = 0;
pos->apply.b = 0;
pos->apply.b = 0;
pos->apply.b = 0;
} i = 0;
for (pos = PCB; pos < pcb+3; pos++) {printf ("Input process%d Max demand max\n", ++i);
scanf ("%d%d%d", &pos->max.a, &pos->max.b, &pos->max.c); while (Pos->max.a>os | | pos->max.b>os | | pos->max.c>os | | pos->max.a<0 | | pos->max.b<0 | |
pos->max.c<0) {printf ("The data input is wrong, please re-enter: \ n");
scanf ("%d%d%d", &pos->max.a, &pos->max.b, &pos->max.c);
} printf ("Input process%d assigned amount \ n", i);
scanf ("%d%d%d", &pos->allocation.a, &pos->allocation.b, &POS->ALLOCATION.C); while (pos->allocation.a>pos->max.a | | pos->allocation.b>pos->max.b | | pos->allocation.c> pos->max.c | | pos->allocation.a<0 | | pos->allocation.b<0 | |
pos->allocation.c<0) {printf ("The data input is wrong, please re-enter: \ n");
scanf ("%d%d%d", &pos->allocation.a, &pos->allocation.b, &POS->ALLOCATION.C);
} a+=pos->allocation.a;
b+=pos->allocation.b;
c+=pos->allocation.c;
POS->NEED.A = pos->max.a-pos->allocation.a;
POS->NEED.B = pos->max.b-pos->allocation.b;
POS->NEED.C = pos->max.c-pos->allocation.c;
} available.a = System.a-a;
available.b = System.b-b; AvAilable.
C = system.c-c;
}*/void Initpcb () {//printf ("Total input system resources: \ n");
SYSTEM.A = 10;
SYSTEM.B = 5;
SYSTEM.C = 7;
int i = 0, a = 0, b = 0, c = 0;
for (pos = PCB; pos < pcb+3; pos++) {i++;
Pos->id = i;
pos->state = ' R ';
Pos->allocation.a = 0;
pos->allocation.b = 0;
POS->ALLOCATION.C = 0;
pos->apply.b = 0;
pos->apply.b = 0;
pos->apply.b = 0;
} i = 0;
Initialize the first pos = PCB;
POS->MAX.A = 7;
pos->max.b = 5;
POS->MAX.C = 3;
Pos->allocation.a = 0;
pos->allocation.b = 1;
POS->ALLOCATION.C = 3;
a+=pos->allocation.a;
b+=pos->allocation.b;
c+=pos->allocation.c;
POS->NEED.A = pos->max.a-pos->allocation.a;
POS->NEED.B = pos->max.b-pos->allocation.b;
POS->NEED.C = pos->max.c-pos->allocation.c;
Initializes a second pos = pos+1;
POS->MAX.A = 4;
pos->max.b = 3;
POS->MAX.C = 3;
Pos->allocation.a = 4;
pos->allocation.b = 1; Pos->allocation.C = 1;
a+=pos->allocation.a;
b+=pos->allocation.b;
c+=pos->allocation.c;
POS->NEED.A = pos->max.a-pos->allocation.a;
POS->NEED.B = pos->max.b-pos->allocation.b;
POS->NEED.C = pos->max.c-pos->allocation.c;
Initializes a third pos = pos+1;
POS->MAX.A = 3;
pos->max.b = 0;
POS->MAX.C = 6;
Pos->allocation.a = 3;
pos->allocation.b = 0;
POS->ALLOCATION.C = 1;
a+=pos->allocation.a;
b+=pos->allocation.b;
c+=pos->allocation.c;
POS->NEED.A = pos->max.a-pos->allocation.a;
POS->NEED.B = pos->max.b-pos->allocation.b;
POS->NEED.C = pos->max.c-pos->allocation.c;
Last available.a = system.a-a;
available.b = System.b-b;
available.c = system.c-c;
} void print () {int i = 0;
printf ("..... ..... ..... .... .... .... ..... .... .... .... .... .... .... .... .... .... .... ..... .... ..... ..... ....... ...)
printf ("\ n Current state: {Available amount: [%d%d%d]}\n\n", Available.a, available.b, available.c); printf ("Process number status The amount of the recent application volume has been allocated in demand \ n "); for (Pos0 = PCB; Pos0 < pcb+3; pos0++) {printf ("%d\t%c [%d]%d [%d]%d] [%d%d%d] [%d] %d%d]\n ", Pos0->id, Pos0->state, Pos0->apply.a, pos0->apply.b, pos0->apply.c, Pos0->Max.A, pos0- >MAX.B, POS0->MAX.C, Pos0->allocation.a, pos0->allocation.b, POS0->ALLOCATION.C, Pos0->Need.A,
POS0->NEED.B, POS0->NEED.C);
} printf ("... ..... ..... ...). ..... ..... ..... ..... ............................. ... \ n.");
} int Bank () {printf ("\n*************************** banker Algorithm *************************************\n\n");
int i = 0;
int ans[3];
for (pos = PCB;; pos++) {pos->apply.a = pos->apply.b = pos->apply.c = 0; if (pos->state = = ' R ') {//The system should calculate the security of this resource allocation before allocating resources to it///If this allocation does not cause the system to enter an unsafe state, the resource can be allocated to the process//security check is reduced prematurely Necessary rollback, unsafe wait if (available.a<pos->need.a | | available.b<pos->need.b | | AVAILABLE.C<POS->NEED.C) {Pos->state= ' W ';
printf ("Request to%d process will result in unsafe state, process%d wait \ n", Pos->id, Pos->id);
if (pos = = pcb+2) pos = pcb-1;
Continue }//Secure redistribution if (pos->need.a==pos->max.a && pos->need.b==pos->max.b && Pos->nee
D.C==POS->MAX.C) printf ("Enter the resource request amount of%d processes \ n", pos->id);
else printf ("Input process%d also requires a resource volume [%d%d], assigning it to the process: \ n", Pos->id, Pos->need.a, pos->need.b, POS->NEED.C);
scanf ("%d%d%d", &pos->apply.a, &pos->apply.b, &pos->apply.c); Not satisfying condition one while (pos->apply.a>pos->need.a | | pos->apply.b>pos->need.b | | pos->apply.c>po
S->NEED.C) {printf ("The resource request amount is greater than the total resource requirement, re-enter: \ n");
scanf ("%d%d%d", &pos->apply.a, &pos->apply.b, &pos->apply.c); }//Do not satisfy the condition two, wait for if (Pos->apply.a>available.a | | pos->apply.b>available.b | | pos->apply .
C>AVAILABLE.C) {pos->state = ' W ';
printf ("The available resource could not meet the requested amount of the resource, process%d waits \ n", pos->id);
if (pos = = pcb+2) pos = pcb-1;
Continue
}//First, second condition met, allocation Pos->allocation.a = pos->allocation.a+pos->apply.a;
pos->allocation.b = pos->allocation.b+pos->apply.b;
POS->ALLOCATION.C = pos->allocation.c+pos->apply.c;
POS->NEED.A = pos->max.a-pos->allocation.a;
POS->NEED.B = pos->max.b-pos->allocation.b;
POS->NEED.C = pos->max.c-pos->allocation.c;
Available.a = available.a-pos->apply.a;
available.b = available.b-pos->apply.b;
AVAILABLE.C = available.c-pos->apply.c; printf ("Assigned to process%d[%d,%d,%d") resources also requires [%d,%d,%d] resources. \ n ", Pos->id, Pos->apply.a, pos->apply.b, POS->APPLY.C,
POS->NEED.A, pos->need.b, POS->NEED.C);
Print (); Occupy equals to TotalRequirements, this process completes if (pos->allocation.a==pos->max.a && pos->allocation.b==pos->max.b && pos-
>ALLOCATION.C==POS->MAX.C) {pos->state = ' E ';
Available.a + = pos->allocation.a;
available.b + = pos->allocation.b;
available.c + = pos->allocation.c;
Pos->allocation.a = pos->allocation.b = POS->ALLOCATION.C = 0;
printf ("process [%d] has got all the resources and freed up the possession of the resource!\n", pos->id);
Print ();
Ans[i] = pos->id;
i++;
if (pos = = pcb+2) pos = pcb-1;
}//All processes are in completion state, output safe sequence TMP = PCB; if (tmp->state== ' E ' && (tmp+1)->state== ' E ' && (tmp+2)->state== ' e ') {print
F ("The banker algorithm is allocated, the system is in a safe state, the security sequence is:%d->%d->%d\n", Ans[0], ans[1], ans[2]);
Print ();
return 0;
}else {if (pos = = pcb+2) pos = pcb-1;
Continue
}} else if (pos->state = = ' W ')//wait for the status of the process to become ready or skip directly {//If the resource is available to meet the process resource request that was originally waiting, it becomes ready if (available.a>pos->apply.a && available.a>=0 && available.b>pos->apply.b && Available.b>=0 && available.c>pos->apply.c && available.c>=0) {PO
s->state = ' R ';
pos--;//becomes ready and must be assigned immediately} else {if (pos = = pcb+2) pos = pcb-1;
Continue
}}}} int random () {printf ("\n*************************** random-order allocation algorithm *************************************\n\n");
int i = 0;
for (pos = PCB;; pos++) {pos->apply.a = pos->apply.b = pos->apply.c = 0; if (pos->state = = ' R ') {if (available.a<=0 | | available.b<=0 | | available.c<=0) {pos->state = ' W ';
printf ("The system resource is not sufficient to temporarily allocate to process [%d], process waits \ n", pos->id);
TMP = PCB; if (tmp->state== ' W ' && (tmp+1)->state== ' W ' && (tmp+2)->state== ' W ')//process is in the completed state {printf ("
Random allocation algorithm generates deadlock!\n ");
return 0;
} if (pos = = pcb+2) pos = pcb-1;
Continue } if (pos->need.a==pos->max.a && pos->need.b==pos->max.b && pos->need.c==pos->
MAX.C) printf ("Enter the number of applications for%d processes \ n", pos->id);
else printf ("Input process%d also requires the amount of resources ([%d]%d]) to assign it to the process: \ n", Pos->id, Pos->need.a, pos->need.b, POS->NEED.C);
scanf ("%d%d%d", &pos->apply.a, &pos->apply.b, &pos->apply.c); Not satisfying condition one while (pos->apply.a>pos->need.a | | pos->apply.b>pos->need.b | | pos->apply.c>po
S->NEED.C) {printf ("The resource request amount is greater than the total resource requirement, re-enter: \ n");
scanf ("%d%d%d", &pos->apply.a, &pos->apply.b, &pos->apply.c); }//Do not satisfy the condition two, wait for if (Pos->apply.a>available.a | | pos->apply.b>available.b | | pos-> ;
APPLY.C>AVAILABLE.C) {pos->state = ' W ';
printf ("The available resource could not meet the requested amount of the resource, process%d waits \ n", pos->id);
TMP = PCB;
All processes are waiting, then deadlock if (tmp->state== ' W ' && (tmp+1)->state== ' W ' && (tmp+2)->state== ' W ') {
printf ("Random allocation algorithm generates deadlock!\n");
return 0;
} if (pos = = pcb+2) pos = pcb-1;
Continue
}//First two conditions are met, allocating resources Pos->allocation.a = pos->allocation.a+pos->apply.a;
pos->allocation.b = pos->allocation.b+pos->apply.b;
POS->ALLOCATION.C = pos->allocation.c+pos->apply.c;
POS->NEED.A = pos->max.a-pos->allocation.a;
POS->NEED.B = pos->max.b-pos->allocation.b; POS->NEED.C = Pos->max.c-pos->allocatiOn.
C
Available.a = available.a-pos->apply.a;
available.b = available.b-pos->apply.b;
AVAILABLE.C = available.c-pos->apply.c; printf ("Assigned to process%d[%d,%d,%d") resources also requires [%d,%d,%d] resources. \ n ", Pos->id, Pos->apply.a, pos->apply.b, POS->APPLY.C,
POS->NEED.A, pos->need.b, POS->NEED.C);
Print (); Occupy equals total demand, this process completes if (pos->allocation.a==pos->max.a && pos->allocation.b==pos->max.b &&A mp
POS->ALLOCATION.C==POS->MAX.C)//possession equals total demand {pos->state = ' E ';
Available.a + = pos->allocation.a;
available.b + = pos->allocation.b;
available.c + = pos->allocation.c;
Pos->allocation.a = pos->allocation.b = POS->ALLOCATION.C = 0;
printf ("process [%d] has got all the resources and freed up the possession of the resource!\n", pos->id);
Print ();
if (pos = = pcb+2) pos = pcb-1;
} TMP = PCB;
if (tmp->state== ' E ' && (tmp+1)->state== ' E ' && (tmp+2)->state== ' e ')//process is in the completed state {
printf ("Random allocation algorithm does not generate deadlock!\n");
Print ();
return 0;
} else {if (pos = = pcb+2) pos = pcb-1;
Continue }} else if (pos->state = = ' W ')///wait for the status of the process {if (available.a>pos->apply.a && AVAILABLE.A&G T;=0 && available.b>pos->apply.b && available.b>=0 && available.c>pos->
APPLY.C && available.c>=0) {pos->state = ' R ';
pos--;
} else {if (pos = = pcb+2) pos = pcb-1;
Continue
}}}} int main () {//Freopen ("In.txt", "R", stdin);
int n = 9999;
printf ("Resource allocation \ n"); while (n! = 0)
{printf ("--------------------------------------------------\ n"); printf ("|
&*****************************& |\n "); printf ("|
* 1, banker algorithm * |\n "); printf ("|
* 2, random allocation algorithm * |\n "); printf ("|
* 0, exit procedure * |\n "); printf ("|
&*****************************& |\n ");
printf ("--------------------------------------------------\ n Please select: \ n");
scanf ("%d", &n);
printf ("\ n");
if (n = = 1) {INITPCB ();
Print ();
Bank ();
} else if (n = = 2) {INITPCB ();
Print ();
Random ();
} else if (n = = 0) {printf ("Thank you for using!\n");
Exit (0);
} else printf ("Select Error, please re-select!\n");
} return 0;
}
Attach a set of test data:
Max Allocation Need Available
P1 7 5 3 0 1 3 7 4 0 3 3 2
P2 4 3 3 4 1 1 0 2 2
P3 3 0 6 3 0 1 0 0 5
Sample Debug Results section: