First: Preface
This question requires knowledge of the security checking algorithm for the previous one, so it is strongly recommended that you look at the previous one first.
7-1 Banker Algorithm - Security Check (20 points)
2: Title
Enter N processes (N<=100) and M-class resources (M<=100), initialize the total number of resources, and the allocation of resources at T0. For example, suppose there are five processes {P 0, P 1, P 2, P 3, P 4} and three types of resources {A,B,C} in the system, and the number of resources is 10, 5, 7, respectively. The resource allocation at T0
Enter the process of applying for resources and the number of resources to be applied for to determine whether or not they are allocated. If assigned, Output finds the security sequence and can be assigned. "And output the assigned system state. If not assigned, the output cannot find the security sequence and will not be assigned." "And output the current system state.
Input format:
The first line enters the number of processes N, the second line enters the number of resource classes M, the third line enters the total number of resource classes M, and the following N lines enter the name of each process, the process's maximum demand for resource class M and the allocated resources. The last line enters the name of the application process and the number of resource classes requested.
Output format:
If assigned, Output finds the security sequence and can be assigned. "And output the assigned system state.
If not, give the reason for not allocating:
1. If the name of the process entered is incorrect, the output "does not find the process. "And output the current system state.
2. If the number of resources requested is greater than the maximum demand, the output demand is unreasonable and will not be allocated. "And output the current system state.
3. If the number of resources requested is greater than the remaining resources, the output "remaining resources are insufficient and will not be allocated. "And output the current system state.
4. If no security sequence is found, the output cannot find the security sequence and will not be assigned. "And output the current system state.
Input Sample 1:
Give a set of inputs here. For example:
5 3 10 5 7 P0 7 5 3 0 1 0 P1 3 2 2 2 0 0 P2 9 0 2 3 0 2 P3 2 2 2 2 1 1 P4 4 3 2 0 0 2 P1 1 0 2 No empty line at end
Output Sample 1:
Give the appropriate output here. For example:
A security sequence can be found and assigned. name max allocation need available P0 7 5 3 | 0 1 0 | 7 4 3 | 2 3 0 P1 3 2 2 | 3 0 2 | 0 2 0 | P2 9 0 2 | 3 0 2 | 6 0 0 | P3 2 2 2 | 2 1 1 | 0 1 1 | P4 4 3 2 | 0 0 2 | 4 3 0 | No empty line at end
Input Sample 2:
Give a set of inputs here. For example:
5 3 10 5 7 P0 7 5 3 0 1 0 P1 3 2 2 2 0 0 P2 9 0 2 3 0 2 P3 2 2 2 2 1 1 P4 4 3 2 0 0 2 P5 1 0 2
No empty line at end
Output Sample 2:
Give the appropriate output here. For example:
The process was not found. name max allocation need available P0 7 5 3 | 0 1 0 | 7 4 3 | 3 3 2 P1 3 2 2 | 2 0 0 | 1 2 2 | P2 9 0 2 | 3 0 2 | 6 0 0 | P3 2 2 2 | 2 1 1 | 0 1 1 | P4 4 3 2 | 0 0 2 | 4 3 0 |
No empty line at end
Three: Ideas
Ideas:
1. I believe that if we get to the point of safety check before AC comes out, it will be done soon too.
2. The need and Avaliable required above are the same as the question
3. So next we'll look at the relationship between requst and need and avaliable
Where requset is the amount of resources requested by a process p1
1>: If the process name is entered incorrectly, the output will not find the process
2>: If the appeal criteria are met, judge the request and Max relationship request <= Max on Oak
Otherwise, the demand for output is unreasonable and shall not be allocated. "And output the current system state.
3>: If the appeal conditions are met, then judge the relationship between request and avaliable request <= avaliable on Oak
Otherwise, the remaining resources are insufficient and will not be allocated.
4>: The allocation and need of the corresponding process will change if the appeal conditions are met
And the avaliable s of the resources available in their systems need to change accordingly
5>: The next step is to determine if the current avaliable is a security state, that is, a security check
If a security sequence is found, the output "A security sequence can be found, and it can be assigned." It also outputs the assigned system state.
Output if not found "Security sequence not found, not assigned." Output current system state.
Four: Top code:
/** Ideas: 1.I'm sure if we get to the point of safety check before AC comes out, it will be done soon too. 2.need and Avaliable are the same as this question 3.So next we'll look at the relationship between requst and need and avaliable Where requset is the amount of resources requested by a process p1 1>: If the process name is entered incorrectly, the output will not find the process 2>: If the appeal criteria are met, judge the request and Max relationship request <= Max on Oak Otherwise the Output requirement is unreasonable and will not be assigned. It will output the current system state. 3>:If the appeal criteria are met, then judge the relationship between request and avaliable request <= avaliable on Oak Otherwise, the remaining resources are insufficient and will not be allocated. "And output the current system state 4>:The allocation and need of the corresponding process will change if the appeal conditions are met And the avaliable s of the resources available in their systems need to change accordingly 5>:The next step is to determine if the current avaliable is a security state, that is, a security check If a security sequence is found, the output "A security sequence can be found, and it can be assigned." It also outputs the assigned system state. Output if not found "Security sequence not found, not assigned." Output current system state. **/ #include<bits/stdc++.h> using namespace std; int N,M; string str; vector<int>v1;//Total amount of resources used to store various kinds vector<int>v2;//The amount of resources requested on behalf of the process requesting resources vector<int>v3;//Used to record the initial avaliable struct Node{ string processName; int a[100];//Max int b[100];//allocation int c[100];//need bool finish; } node[1000]; //Used to determine if there is a process bool judgeExit(string str){ for(int i = 0; i < N; i++){ if(str == node[i].processName){ return true; } } return false; } //request and need for calculating this time bool requestNeed(){ int count = 0; for(int i = 0; i < N; i++){ int count = 0; if(str == node[i].processName){ for(int j = 0; j < M; j++){ if(v2[j] <= node[i].c[j]){//Request volume is less than need count++; } } } if(count == M){ return true; } } return false; } //Use to determine if the number of resources requested is greater than the remaining resources bool requestAvaliable(){ int count = 0; for(int j = 0; j < M; j++){ if(v2[j] <= v1[j]){ //Requests less than avaliabale count++; } } if(count == M){ return true; } return false; } //Determine the current security state bool isSafe(){ int cnt = 0; for(int i = 0; i < N; i++){ int count = 0; for(int j = 0; j < M; j++){ if(node[i].c[j] <= v1[j]){ count++; }else{ break;//break out whenever there's an inappropriate one } } if(node[i].finish == false && count == M) {//count == M indicates that the remaining total amount of each resource is greater than what the process needs for(int j = 0; j < M; j++){ v1[j] += node[i].b[j];//Then the total amount of remaining resources is the same as before plus the process releases the resources it occupies } node[i].finish = true; cnt++;//Record the number of completed processes // cout << node[i].processName << ' '; //The advantage here is that as long as we find the right one, we continue to look for the right one i = -1; } } if(cnt == N){ return true; } return false; } //Used to output the state of the system at this time void state(){ int flag = 0; cout << "name max allocation need available" << endl; for(int i = 0; i < N; i++){ cout << node[i].processName << ' '; for(int j = 0; j < M; j++){ cout << node[i].a[j] << ' '; } cout << "| "; for(int j = 0; j < M; j++){ cout << node[i].b[j] << ' '; } cout << "| "; for(int j = 0; j < M; j++){ cout << node[i].c[j] << ' '; } cout << "|"; if(flag == 0){ for(int j = 0; j < M; j++){ if(j == 0) cout << ' ' <<v3[j]; else cout << ' ' <<v3[j] ; } flag = 1; } cout << endl; } } int main(){ cin >> N >> M; for(int i = 0; i < M; i++){ int nums; cin >> nums; v1.push_back(nums); } for(int i = 0; i < N; i++){ cin >> node[i].processName; //Input Max for(int j = 0; j < M; j++){ cin >> node[i].a[j]; } //Enter allovation for(int j = 0; j < M; j++){ cin >> node[i].b[j]; v1[j] -= node[i].b[j];//This is where the allocated resources are subtracted each time, then the last remaining is available } //Calculate need for(int j = 0; j < M; j++){ node[i].c[j] = node[i].a[j] - node[i].b[j]; } node[i].finish = false; } cin >> str; for(int i = 0; i < M; i++){ int temp; cin >> temp; v2.push_back(temp); } //Record the initial avaliable for(int i = 0; i < M; i++){ v3.push_back(v1[i]); } if(judgeExit(str) == false){ cout << "The process was not found." << endl; state(); }else if(requestNeed() == false){ cout << "The demand is unreasonable and shall not be distributed." << endl; state(); }else if(requestAvaliable() == false) { cout << "The remaining resources are insufficient to be allocated." << endl; state(); } //Start assigning the requested resource to the process and perform security checks at the same time else if(judgeExit(str) == true && requestNeed() == true && requestAvaliable() == true){ for(int i = 0; i < N; i++){ if(str == node[i].processName){ for(int j = 0; j < M; j++){ node[i].b[j] += v2[j];//Here is the allocation to update the process node[i].c[j] -= v2[j];//Here is the need to update the process v1[j] -= v2[j]; //Here is avaliable to update the process v3[j] -= v2[j];//Store the original avaliable } } } if(isSafe() == true){ cout << "A security sequence can be found and assigned." << endl; state(); }else{ cout << "No security sequence was found and will not be assigned." << endl; //If the conditions are not met, the resource allocation from the original process needs to be output for(int i = 0; i < N; i++){ if(str == node[i].processName){ for(int j = 0; j < M; j++){ node[i].b[j] -= v2[j];//Here is the allocation to update the process node[i].c[j] += v2[j];//Here is the need to update the process v1[j] += v2[j]; //Here is avaliable to update the process v3[j] += v2[j];//Store the original avaliable } } } state(); } } } //5 //3 //10 5 7 //P0 7 5 3 0 1 0 //P1 3 2 2 2 0 0 //P2 9 0 2 3 0 2 //P3 2 2 2 2 1 1 //P4 4 3 2 0 0 2 //P1 2 2 2 //name max allocation need available //P0 7 5 3 | 0 1 0 | 7 4 3 | 3 3 2 //P1 3 2 2 | 2 0 0 | 1 2 2 | //P2 9 0 2 | 3 0 2 | 6 0 0 | //P3 2 2 2 | 2 1 1 | 0 1 1 | //P4 4 3 2 | 0 0 2 | 4 3 0 | //Security sequence found, in a safe state. //5 //3 //10 5 7 //P0 7 5 3 0 1 0 //P1 3 2 2 2 0 0 //P2 9 0 2 3 0 2 //P3 2 2 2 2 1 1 //P4 4 3 2 0 0 2 //P0 0 1 0
Babies come on, we reluctantly get the confidence to go on smoothly and stick to love what we were meant to be!!!!!!!!!!!!!