C ++ for bankers

Source: Internet
Author: User

T0 time system status table:
Maximum resource demand
Number of allocated resources
A B C
A B C
P1
5 5 9
2 1 2
P2
5 3 6
4 0 2
P3
4 0 11
4 0 5
P4
4 2 5
2 0 4
P5
4 2 4
3 1 4
Total resources: A 17 B 5 C 20
The security detection principle is:
1. Process application resources can be distributed, but the total amount should not exceed the maximum demand.
2. The amount of resources requested each time must be smaller than the amount of resources available to the system
3. After the allocation is completed, the system is secure, that is, there is still a sequence that can run all processes at present. This is the key
I wrote a program to demonstrate the entire test process. I can understand the banker's algorithm by taking a look at the running results. The Code passes the test perfectly under vc6.
Complete code and comments are attached:
[Cpp]
# Include <iostream>
Using namespace std;
 
# Define M 5 // number of processes
# Define N 3 // number of resources
 
Int R [N] = {17, 5, 20}; // total number of resources
Int Maxall [N] = {0}; // number of occupied Resources
Int V [N] = {0}; // total number of available resources
 
Int Max [M] [N] = {5, 5, 9}, {5, 3, 6}, {4, 0, 11}, {4, 2, 5}, {4, 2, 4 }}; // maximum number of processes required
Int Alloc [M] [N] = {2, 1, 2}, {4, 0, 2}, {4, 0, 5}, {2, 0, 4}, {3, 1, 4 }}; // resources allocated by various processes
Int Need [M] [N] = {0}; // amount of resources required by each process
Int Appli [M] [N] = {0}; // The amount of resources requested by a process at a time
 
Int I, j, n; // used in the program
 
 
 
Int main ()
{
Void init ();
Void show ();
Int applicate (int, int );
Void update (int, int );
Void release (int, int );
Int safecheck ();
Init ();
Show ();
Safecheck (); // The system status before and after the security sequence is completed
Show ();
Return 0;
}
 
Void init () // initialization status
{
/* Cout <"Enter the maximum resource demand of each process in sequence:" <endl;
For (I = 0; I <M; I ++)
For (j = 0; j <N; j ++)
{
Cout <I <"process" <j <"Resource:" <endl;
Cin> Max [I] [j];
}
Cout <"Enter the allocated resources of each process in sequence:" <endl;
For (I = 0; I <M; I ++)
For (j = 0; j <N; j ++)
{
Cout <I <"process" <j <"Resource:" <endl;
Cin> Alloc [I] [j];
}
Cout <"Enter the total number of resources in sequence:" <endl;
For (j = 0; j <N; j ++)
{
Cout <j <"Resource:" <endl;
Cin> R [j];
}
*/
// This can be input by yourself. To avoid a large amount of input, initialize it in the program.
For (I = 0; I <M; I ++)
For (j = 0; j <N; j ++)
Need [I] [j] = Max [I] [j]-Alloc [I] [j];
 
For (j = 0; j <N; j ++)
For (I = 0; I <M; I ++)
Maxall [j] + = Alloc [I] [j];
 
For (j = 0; j <N; j ++)
V [j] = R [j]-Maxall [j];
}
 
Void show () // display the system status
{
Cout <"Max:" <endl;
For (I = 0; I <M; I ++)
{
For (j = 0; j <N; j ++)
Cout <Max [I] [j] <"";
Cout <endl;
}
Cout <endl;
Cout <"Alloc:" <endl;
For (I = 0; I <M; I ++)
{
For (j = 0; j <N; j ++)
Cout <Alloc [I] [j] <"";
Cout <endl;
}
Cout <endl;
Cout <"Need:" <endl;
For (I = 0; I <M; I ++)
{
For (j = 0; j <N; j ++)
Cout <Need [I] [j] <"";
Cout <endl;
}
Cout <endl;
Cout <"total system resources:" <endl;
For (n = 0; n <N; n ++)
Cout <R [n] <"";
Cout <endl;
Cout <"remaining system resources" <endl;
For (j = 0; j <N; j ++)
{
Cout <V [j] <"";
}
Cout <endl;
}
 
Int applicate (int I, int r1, int r2, int r3) // apply for a resource
{
Appli [I] [0] = r1;
Appli [I] [1] = r2;
Appli [I] [2] = r3;
If (Appli [I] [0] <= Need [I] [0] & Appli [I] [1] <= Need [I] [1] & Appli [I] [2] <= Need [I] [2]) & (Appli [I] [0] <= V [0] & Appli [I] [1] <= V [1] & Appli [I] [2] <= V [2])
{
// Cout <"application successful, but security detection is not performed..." <endl;
Return 1;
}
Else
{
// Cout <"application failed..." <endl;
Return 0;
}
}
 
Void update (int I, int r1, int r2, int r3) // allocate resources and ensure data consistency.
{
Alloc [I] [0] + = r1; Alloc [I] [1] + = r2; Alloc [I] [2] + = r3;
Need [I] [0]-= r1; Need [I] [1]-= r2; Need [I] [2]-= r3;
V [0]-= r1; V [1]-= r2; V [2]-= r3;
}
 
Void release (int I, int r1, int r2, int r3) // release resources and ensure data consistency.
{
Alloc [I] [0]-= r1; Alloc [I] [1]-= r2; Alloc [I] [2]-= r3;
Need [I] [0] + = r1; Need [I] [1] + = r2; Need [I] [2] + = r3;
V [0] + = r1; V [1] + = r2; V [2] + = r3;
}
 
Int safecheck () // checks the security, repeatedly searches for an executable process M times, releases resources after execution, and then repeats the next step.
{
Int count = 0;
Cout <endl;
Cout <"begins the Security Test: (this test changes the data, that is, the entire process is executed, and the formal detection should have backup data. Here only the detection process is demonstrated, which is omitted to save space) "<endl;
While (count <5)
{
For (int m = 0; m <M; m ++)
{
If (applicate (m, Need [m] [0], Need [m] [1], Need [m] [2])
{
If (Alloc [m] [0] = 0) & (Alloc [m] [1] = 0) & (Alloc [m] [2] = 0 )))
Continue;
Update (m, Need [m] [0], Need [m] [1], Need [m] [2]);
Release (m, Alloc [m] [0], Alloc [m] [1], Alloc [m] [2]);
Cout <"process" <m <"Successful Completion" <endl;
Count ++;
Show (); // you can observe every detailed change.
Break;
}
}
}
Cout <"Now the queue is finished! "<Endl;
Return 1;
}
By goodcat12

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.