Branch algorithm realization

xiaoxiao2021-03-06  64

Special declaration: reprinted a big brother's program. Algorithm introduction: ** Data Architecture: 1. Available ValiAile2. Maximum Demand Matrix MAX3. Allocation Matrix Allocation4. Demand Matrix NEED ** Function: Simulation DIJKSTRA's banker algorithm to avoid deadlocks. Composition: Part 1: Bank algorithm (scan) 1. If Request <= NEED, turn to 2; otherwise, error 2. If request <= available, turn to 3, otherwise wait 3. The system temoss the resource allocated to the process 4. System execution security algorithm 2: Security algorithm 1. Set two vector (1). Work vector: work = available (indicating the number of resources required to continue running to process) (2) .finish : Indicates if the system has enough resources to assign to the process (True: Yes; false: no). Initialization is false2. If Finish [i] = false && need <= work, execute 3; otherwise, execute 4 (i is resource category) 3. The process P obtains the Class I resource, then executes until it is complete! And release resources: work = work allocation; finish [i] = true; turn 24. If all processes are finished [i] = true, the system is secured; Otherwise, not safe!

II. Original code and notes: #include #include #include #include "windows.h" #define max_process 32 // maximum number of processes #define max_cource 64 // Maximum resource category

INT MAX_FACT_PROCESS; // Real Total Process Number INT MAX_FACT_COURCE; / / Actual Resource Category Int Available [MAX_COURCE]; // Available Resource Vector Int Max [MAX_PROCESS] [MAX_COURCE]; // Maximum Demand Matrix Int Allocation [MAX_PROCESS] [Max_Process] [ Max_cource]; // Assign Matrix INT NEED [MAX_PROCESS] [MAX_COURCE]; // Demand Matrix

INT Request_Process; // Send a request for the process INT request_cource; // Request resource category int request_cource_nember; // Request the number of resources

Struct Comp {Int value; Int num; int next;}; int flag = 0; void read_initiate (void) {// read initialization document ifstream infile ("initiate.txt"); if (! infile) {cout << "You can't open the input file:" << "initiate.txt" << '/ n'; exit (1);} cout << "Start reading initialization document" << '/ n'; int CH; int Array [ Max_process * max_cource * 2]; int Num = 0; while (infile >> CH) Array [Num ] = ch; Num = 0; Max_FACT_COURCE = Array [Num ]; for (int J = 0; j

Num = 0; OUTFILE << Array [NUM ] << ""; for (i = 0; i > CH) Array [Num ] = CH; Num = 0; for (INT i = 0 i

Void set_need (void) {// Setup demand matrix cout << "setting requirements matrix" << '/ n'; for (int i = 0; i

Void Read_Request (Void) {// Read Request Vector Ifstream Infile ("Request_List.txt"); if (! infile) {cout << "You cannot open the input file:" << "Request_list.txt" << '/ n '; EXIT (1);} cout << "Start reading request vector" <<' / n '; int Array [3]; int Num = 0, ch; while (infile >> CH) array [num ] = CH; request_process = array [0]; request_cource = array [1]; request_cource_nember = array [2]; infile.close ();} void write_allocation (void) {// Modify resource allocation list (resource allocation) OFStream outfile (" Allocated_list.txt "); if (! Outfile) {cout <<" You cannot open the resource allocation list: "<< '/ n'; exit (1);} for (int i = 0; i

Void allocate_source (void) {// Start assignment (have been detected by scanning and security) cout << '/ n' << "start to" << Request_cource << " Class Resources << Request_cource_Nember << "<< endl; write_initiate (); write_allocation (); dword m_delay = 3000; sleep (m_delay); coup << '/ n' <<" congratulations, resource allocation has been successful ! "<< Endl;}

Void test_safty () {// security detection cout << '/ n' << "Enter security detection!" << endl; int work [max_cource]; for (int i = 0; i = array [j] .value) {INT T; T = array; array [j] .value = array [i] .value; array [i] .value = t; t = array [j] .num; array [j] .num; array [j] nunum = array [ I] .num; array [i] .num = T;} else continue;} dword m_delay = 3000; sleep (m_delay); / * for (i = 0; i

Work [Request_cource-1] Allocation [Array [i] .num] [Request_cource-1]; Finish [Array [i] .num] [Request_cource-1] = true;}} for (i = 0; i "; for (i = 0; i ";} Cout << '/ n' << "has passed security test!" << endl; allocate_source ();} void run (void) {// executive banker algorithm

COUT << "************************************************************ *** "<< '/ n' <<" Click 1 to execute! "<< '/ n' <<" Click 2 to exit! "<< '/ n' <<" ******** **************************************** "<< Endl; CIN >> FLAG; IF (FLAG == 2) EXIT (0); if (FLAG == 1) {cout << "Start Scanning Request Information!" << Endl; DWORD M_DELAY = 3000; Sleep (m_delay); if (Request_cource_Nember> NEED [ Request_process-1] [Request_cource-1]) {cout << '/ n' << "" << Request_Process << "Process Request Press" << Request_cource << "Resource" << Request_cource_Nember << " "<< Endl; cout <<" has exceeded the maximum number of resources required for this process, so it is not assigned! "<< end1; exit (0);} if (request_cource_nember> available [request_cource-1 ]) {cout << '/ n' << "<< Request_Process <<" Process Request Press "<< Request_cource <<" Resource << Request_cource_Nember << "" << Endl; Cout << "But there is no sufficient resource in the system, so enter the waiting queue !!" << Endl; exit (0);} else {available [request_cource-1] = available [request_cource-1] -request_cource_nember; allocation [request_process-1 ] [Request_COURCE-1] = Allocation [Request_PROCESS-1] [Request_COURCE-1] Request_COURCE_NEMBER; Need [Request_PROCESS-1] [Request_COURCE-1] = Need [Request_PROCESS-1] [Request_COURCE-1] -Request _COURCE_NEMBER; COUT << "Scan" << Endl; Sleep (m_delay); test_safty ();}} else {cout << "Enter an error, please re-enter!" << '/ n'; run (); }

Void main (void) {read_initiate (); cout << max_fact_cource << '/ t'; for (int i = 0; i

Set_need (); for (i = 0; i

Read_request (); cout << '/ n' << "" << Request_Process << "Process Request Press" << Request_cource << "Resources << Request_cource_Nember <<" "<< Endl; Cout < <'/ n' << "Read success" << '/ n';

Run ();

转载请注明原文地址:https://www.9cbs.com/read-89323.html

New Post(0)