Banker algorithm C ++ source code and Flowchart

Source: Internet
Author: User

# Include <iostream> <br/> using namespace STD; <br/> # define I 50/* Maximum number of processes */<br/> # define J 100/* Maximum number of resources */<br/> # define false 0 <br /># define true 1 <br/> int available [J]; /* Number of available resources */<br/> int MAX [I] [J]; /* maximum requirement matrix */<br/> int allocation [I] [J];/* allocation matrix */<br/> int need [I] [J]; /* requirement matrix */<br/> int request [I] [J];/* Number of resources required by processes */<br/> int finish [I]; <br/>/* whether the system has sufficient resource allocation */<br/> int P [I];/* record sequence */<br/> int M, N; /* m processes, N Resources */<Br/> void initial (); <br/> bool safe (); <br/> void request (); <br/> void initial () /* initialization algorithm */<br/> {<br/> int I, j; <br/> cout <"Number of input processes :"; <br/> CIN> m; <br/> cout <"number of types of input resources:"; <br/> CIN> N; <br/> cout <"Enter the resources required by each process at most:" <Endl; <br/> for (I = 0; I <m; I ++) <br/> for (j = 0; j <n; j ++) <br/> CIN> MAX [I] [J]; <br/> cout <"Enter the number of resources allocated by each process:" <Endl; <br/> for (I = 0; I <m; I ++) <br/>{< br/> for (j = 0; j <n; j ++) <br/>{< br/> Cin> allocation [I] [J]; <br/> need [I] [J] = MAX [I] [J]-allocation [I] [J]; <br/> If (need [I] [J] <0) <br/> {<br/> cout <"the number you entered" <I + 1 <"Number of processes owned" <j + 1 <"The number of resources is incorrect, enter "<Endl; <br/> j --; <br/> continue; <br/>}< br/> cout <"Enter the current number of resources:" <Endl; <br/> for (I = 0; I <n; I ++) <br/>{< br/> CIN> available [I]; <br/>}< br/> void request ()/* Banker algorithm */<br/> {<br/> int I, ID; <br/> char again; <br/> whi Le (1) <br/>{< br/> cout <"Enter the process ID of the resource to be applied" <Endl; <br/> CIN> ID; <br/> cout <"number of resources requested by the input process:" <Endl; <br/> for (I = 0; I <N; I ++) <br/>{< br/> CIN> request [ID] [I]; <br/>}< br/> for (I = 0; I <n; I ++) <br/>{< br/> If (request [ID] [I]> need [ID] [I]) <br/>{ <br/> cout <"the number of requested resources exceeds the maximum number of required resources! Enter again! "<Endl; <br/> continue; <br/>}< br/> If (request [ID] [I]> available [I]) <br/>{< br/> cout <"there are not enough resources available! Enter again! "<Endl; <br/> continue; <br/>}< br/> for (I = 0; I <n; I ++) <br/>{< br/> available [I]-= request [ID] [I]; <br/> allocation [ID] [I] + = request [ID] [I]; <br/> need [ID] [I]-= request [ID] [I]; <br/>}< br/> If (safe ()) <br/>{< br/> cout <"agree to the allocation request! "<Endl; <br/>}< br/> else <br/>{< br/> cout <" the request is rejected! "<Endl; <br/> for (I = 0; I <n; I ++) <br/>{< br/> available [I] + = request [ID] [I]; <br/> allocation [ID] [I]-= request [ID] [I]; <br/> need [ID] [I] + = request [ID] [I]; <br/>}< br/> for (I = 0; I <m; I ++) <br/>{< br/> finish [I] = 0; <br/>}< br/> cout <"request allocation again by Y/y, no. Press another key. "<Endl; <br/> CIN> again; <br/> If (again = 'y' | again = 'y ') <br/>{< br/> continue; <br/>}< br/> break; <br/>}< br/> bool safe () /* security algorithms */<Br/> {<br/> int I, J, K, L = 0; <br/> int work [J]; /* Work array */<br/> for (I = 0; I <n; I ++) <br/> work [I] = available [I]; <br/> for (I = 0; I <m; I ++) <br/>{< br/> finish [I] = 0; <br/>}< br/> for (I = 0; I <m; I ++) <br/>{< br/> If (finish [I] = 1) <br/>{< br/> continue; <br/>}< br/> else <br/> {<br/> for (j = 0; j <n; j ++) <br/>{< br/> If (need [I] [J]> work [J]) <br/>{< br/> break; <br/>}< br/> If (j = N) <br/>{< br/> F Inish [I] = 1; <br/> for (k = 0; k <n; k ++) <br/> {<br/> work [k] + = allocation [I] [k]; <br/>}< br/> P [L ++] = I; <br/> I =-1; <br/>}< br/> else <br/> {www.lwfree.cn <br/> continue; <br/>}< br/> If (L = m) <br/>{ <br/> cout <"the system is secure" <Endl; <br/> cout <"security sequence:" <Endl; <br/> for (I = 0; I <L; I ++) <br/>{< br/> cout <p [I]; <br/> if (I! L-1) <br/>{< br/> cout <""; <br/>}< br/> cout <"" <Endl; <br/> return 1; <br/>}< br/> cout <"the system is insecure" <Endl; <br/> return 0; <br/>}< br/> int main () <br/>{< br/> initial (); <br/> safe (); <br/> request <br/>}1. design purpose
1) understand the resource allocation for concurrent execution of multiple processes in multiple program systems.
2) master the Banker algorithm and understand the resource allocation in the concurrent execution of the process.
3) master the methods for preventing deadlocks and the basic concepts of system security status.
2. Design Content
Design a program where n concurrent processes share M system resources to implement the Banker algorithm. Requirements:
1) Simple selection interface;
2) displays the occupied and remaining resources of the current system.
3) Allocate resources to processes. If the required resources are greater than the remaining resources of the system, they are not allocated and prompt that the allocation is unsuccessful;
4) cancel a job and release resources.
Write and debug a simple simulation program for the system to dynamically allocate resources, observe the conditions for deadlock, and adopt appropriate algorithms to effectively prevent and avoid deadlocks.
The principle of resource allocation by bankers algorithms is that the system determines the maximum resource demand of each process. When a process requests to apply for resources, the system tests the maximum number of resources required by the process, if the number of existing resources in the system is greater than or equal to the maximum number of resources required by the process, the current application of the process is met. In this way, at least one process may get all resources and execute until the end, and then return all the resources it occupies for other processes to use.
Data Structure in bankers Algorithms
(1) available resource vector available (one-dimensional array)
Is an element containing M, each of which represents the number of resources available for a class, the initial value is the number of all available resources for the class configured in the system. If available [J] = K, it indicates k Existing RJ resources in the system.
(2) max (two-dimensional array)
M matrix, which defines the maximum requirement of each process in the system for M-type resources. If Max (I, j) = K, the maximum number of RJ resources required by process I is K.
(3) allocation matrix allocation (two-dimensional array)
M matrix, which defines the number of resources allocated to each process for each type of resources in the system. If allocation (I, j) = K, process I is allocated with K RJ resources.
(4) Demand Matrix need (two-dimensional array)
Is a matrix containing N * m, used to represent the number of resources required by each process. If need (I, j) = K, it means that process I still needs K RJ resources to complete its tasks.
Need (I, j) = max (I, j)-allocation (I, j)
3.1 program flowchart
Initialize algorithm flowchart bankers algorithm Flowchart
Security Algorithm flowchart 3.2 source code
# Include <iostream> <br/> using namespace STD; <br/> # define I 50/* Maximum number of processes */<br/> # define J 100/* Maximum number of resources */<br/> # define false 0 <br /># define true 1 <br/> int available [J]; /* Number of available resources */<br/> int MAX [I] [J]; /* maximum requirement matrix */<br/> int allocation [I] [J];/* allocation matrix */<br/> int need [I] [J]; /* requirement matrix */<br/> int request [I] [J];/* Number of resources required by processes */<br/> int finish [I]; <br/>/* whether the system has sufficient resource allocation */<br/> int P [I];/* record sequence */<br/> int M, N; /* m processes, N Resources */<Br/> void initial (); <br/> bool safe (); <br/> void request (); <br/> void initial () /* initialization algorithm */<br/> {<br/> int I, j; <br/> cout <"Number of input processes :"; <br/> CIN> m; <br/> cout <"number of types of input resources:"; <br/> CIN> N; <br/> cout <"Enter the resources required by each process at most:" <Endl; <br/> for (I = 0; I <m; I ++) <br/> for (j = 0; j <n; j ++) <br/> CIN> MAX [I] [J]; <br/> cout <"Enter the number of resources allocated by each process:" <Endl; <br/> for (I = 0; I <m; I ++) <br/>{< br/> for (j = 0; j <n; j ++) <br/>{< br/> Cin> allocation [I] [J]; <br/> need [I] [J] = MAX [I] [J]-allocation [I] [J]; <br/> If (need [I] [J] <0) <br/> {<br/> cout <"the number you entered" <I + 1 <"Number of processes owned" <j + 1 <"The number of resources is incorrect, please enter again: "<Endl; <br/> j --; <br/> continue ;}} <br/>}< br/> cout <"Enter the number of existing resources:" <Endl; <br/> for (I = 0; I <N; I ++) <br/>{< br/> CIN> available [I]; <br/>}< br/> void request () /* Banker algorithm */<br/> {<br/> int I, ID; <br/> char again; <br/> while (1) <br/> {<Br/> cout <"Enter the process Number of the resource to be applied for" <Endl; <br/> CIN> ID; <br/> cout <"number of resources requested by the input process:" <Endl; <br/> for (I = 0; I <N; I ++) <br/>{< br/> CIN> request [ID] [I]; <br/>}< br/> for (I = 0; I <n; I ++) <br/>{< br/> If (request [ID] [I]> need [ID] [I]) <br/>{ <br/> cout <"the number of requested resources exceeds the maximum number of required resources! Enter again! "<Endl; <br/> continue; <br/>}< br/> If (request [ID] [I]> available [I]) <br/>{< br/> cout <"there are not enough resources available! Enter again! "<Endl; <br/> continue; <br/>}< br/> for (I = 0; I <n; I ++) <br/>{< br/> available [I]-= request [ID] [I]; <br/> allocation [ID] [I] + = request [ID] [I]; <br/> need [ID] [I]-= request [ID] [I]; <br/>}< br/> If (safe ()) <br/>{< br/> cout <"agree to the allocation request! "<Endl; <br/>}< br/> else <br/>{< br/> cout <" the request is rejected! "<Endl; <br/> for (I = 0; I <n; I ++) <br/>{< br/> available [I] + = request [ID] [I]; <br/> allocation [ID] [I]-= request [ID] [I]; <br/> need [ID] [I] + = request [ID] [I]; <br/>}< br/> for (I = 0; I <m; I ++) <br/>{< br/> finish [I] = 0; <br/>}< br/> cout <"request allocation again by Y/y, no. Press another key. "<Endl; <br/> CIN> again; <br/> If (again = 'y' | again = 'y ') <br/>{< br/> continue; <br/>}< br/> break; <br/>}< br/> bool safe () /* security algorithm */<br/>{< br/> int I, J, K, L = 0; <br/> int work [J]; /* Work array */<br/> for (I = 0; I <n; I ++) <br/> work [I] = available [I]; <br/> for (I = 0; I <m; I ++) <br/>{< br/> finish [I] = 0; <br/>}< br/> for (I = 0; I <m; I ++) <br/>{< br/> If (finish [I] = 1) <br/>{< br/> continue; <br/>}< br/> else <br/> {<br/> for (j = 0; j <n; j ++) <br/>{< br/> If (need [I] [J]> work [J]) <br/>{< br/> break; <br/>}</P> <p>

 

1. Test Data and experiment results

2. Result Analysis and Experiment experience
In this experiment, the banker algorithm is simulated to avoid deadlocks. The Banker algorithm is a typical algorithm in the operating system to avoid deadlocks. The program I designed contains three parts: Data Structure initialization, Banker algorithm, and security algorithm. During initialization, the program needs to use the available resource vector available [J] and max [I. j]. Allocation matrix allocation [I, j]. Requirement Matrix need [I, j]. There is a certain relationship between them. Need [I, j] = MAX [I, j]-allocation [I, j] requires the Banker algorithm to request resources, security algorithms are used to check resource allocation. By combining the three major blocks, we can avoid deadlocks.
Through this experiment, I learned more about the bankers algorithm and the usage of the data structure. During the experiment, I deeply realized the significance of cooperation, I encountered some difficulties. Through repeated thinking and understanding of the knowledge learned in the book and mutual discussion with my classmates, I finally understood the banker's algorithm, and implement it in C ++. In the future, I will study this course harder. Although the course design time was too hasty, I still learned a lot of practical knowledge. In addition to having a better understanding of this algorithm and reviewing the C language, there are a lot of knowledge points in the process, so I would like to thank my teachers and classmates who have helped me in this process.
3 questions
1) understand how to avoid deadlocks that are not often used in the current computer system?
2) How does the current computer system solve the deadlock problem?

 

Related Article

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.