Operating system banker algorithm

xiaoxiao2021-03-06  73

/ * Author: Central China Normal University SunJunyi * // * Compiler: Turboc2.0 * // * The basic idea of ​​the algorithm: backtracking * / # include #include #include #define n 10 / * process number * / # define M 10 / * resource type maximum value * / typef struct {INT IP; / * Current executable process stack stack top pointer * / int Able [n]; / * Current executable process stack * / int Remain [m]; / * Remix array of resources for each resource * / int visited [n]; / * Currently run and release process logo array * / } Worknode; / * Working node data structure * /

INT ASS [N], NEED [N], R [M], PNUM, RNUM, TOP = -1; / * ASS is the allocation matrix of processes and resources, NEED is the demand matrix of processes and resources. * / Worknode current, stack [n]; / * Current For the current working node, Stack is the working node stack * /

Void add (int [】, int []); / * vector add * / void init (); / * Initialization * / void output (); / * Output result * / int small (int [】, int [] ); / * Vector comparison * / int BankAlgo (); / * Bank algorithm * /

INT main () {int CT = 0; CLRSCR (); init (); CT = BankAlgo (); if (ct) Printf ("/ NIT IS SAFE, AND IT HAS% D Solutions / N", CT); ELSE Printf ("/ nit is unsafe / n"); return 0;}

Void init () {INT I, J; Printf ("Process Number:"); Scanf ("% D", & PNUM); Printf ("Resource Number:"); Scanf ("% D", & RNUM); Printf "Resource Series:"); for (i = 0; i

Void add (int x [】, int y []) {INT i; for (i = 0; i

INT Small (int x [】, int y []) {INT i; for (i = 0; i y [i]) Break; if (i == rnum) Return 1; Return 0;} void output () {INT i; for (i = 0; i ", stack [i] .able [stack [i]. IP]); Printf ("p% d / n", stack [i] .able [stack [i] .ip]);} int BankAlgo () {INT i, J, SUM = 0, CT = 0; CURRENT .ip = -1; MEMSET (Current.Visited, 0); for (i = 0; i -1) current = stack [top--]; if (TOP == -1 && current.ip == 0) BREA K; Else Current.ip--;}} Return CT;} The test results are as follows (Andrews S.TANENBAUM "Modern Handling System" 179)

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

New Post(0)