Banker algorithm in Operating System

Source: Internet
Author: User

/* Author: sunjunyi, Huazhong Normal University */
/* Compiler: turboc2.0 */
/* algorithm basic idea: Backtracking Method */
# include
# include
# include
# define N 10/* Maximum number of processes */
# define M 10/* Maximum number of resource types */
typedef struct {< BR> int IP address; /* stack top pointer of the currently executable process stack */
int able [N]; /* Currently executable process stack */
int remain [m];/* remaining array of each resource */
int visited [N]; /* array of processes currently running and released */
} worknode;/* worker node data structure */

Int ass [N] [m], need [N] [m], R [m], pnum, rnum, Top =-1; /* ass is the allocation matrix of processes and resources, and need is the requirement matrix of processes and resources */
Worknode current, stack [N];/* current is the current worker node, stack is the worker node stack */

Void add (INT [], int []);/* vector addition */
Void Init ();/* initialization */
Void output ();/* output result */
Int small (INT [], int []);/* vector comparison */
Int bankalgo ();/* Banker 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 <rnum; I ++) scanf ("% d", R + I );
Printf ("assigned matrix:/N ");
For (I = 0; I <pnum; I ++ ){
Printf ("P % d:", I );
For (j = 0; j <rnum; j ++) scanf ("% d", & ass [I] [J]);
}
Printf ("needed matrix:/N ");
For (I = 0; I <pnum; I ++ ){
Printf ("P % d:", I );
For (j = 0; j <rnum; j ++) scanf ("% d", & need [I] [J]);
}
}

Void add (int x [], int y []) {
Int I;
For (I = 0; I <rnum; I ++)
X [I] + = Y [I];
}

Int small (int x [], int y []) {
Int I;
For (I = 0; I <rnum; I ++)
If (X [I]> Y [I]) break;
If (I = rnum) return 1;
Return 0;
}

Void output (){
Int I;
For (I = 0; I <top; I ++)
Printf ("P % d -->", 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, sizeof (current. Visited), 0 );
For (I = 0; I <rnum; I ++ ){
For (j = 0; j <pnum; j ++)
Sum + = ass [J] [I];
Current. Remain [I] = R [I]-sum;
Sum = 0;
}
For (I = 0; I <pnum; I ++ ){
If (small (need [I], current. Remain ))
Current. Able [++ current. IP] = I;
}
If (current. IP =-1) return 0;
While (1 ){
Stack [++ top] = current;
Current. visited [current. Able [current. IP] = 1;
Add (current. Remain, ass [current. Able [current. IP]);
Current. IP =-1;
For (I = 0; I <pnum; I ++ ){
If (! Current. visited [I] & small (need [I], current. Remain ))
Current. Able [++ current. IP] = I;
}
If (current. IP =-1 ){
If (Top = pnum-1 ){
Output ();
CT ++;
}
Current = stack [top --];
While (current. IP = 0 & top>-1) Current = stack [top --];
If (Top =-1 & Current. IP = 0) break;
Else current. IP --;
}
}
Return CT;
}
The test results are as follows (Andrews S. Tanenbaum's modern operating system page 1)

process number: 5
resource number: 4
resource series: 6 3 4 2
assined matrix:
P0: 3 0 1 1
P1: 0 1 0
P2: 1 1 0
P3: 1 1 0 1
P4: 0 0 0 0
needed matrix:
P0: 1 0 0
P1: 0 1 2
P2: 3 1 0 0
P3: 0 0 1 0
P4: 2 1 1 0
P3 --> P4 --> P0 --> P2 --> P1
P3 --> P4 --> P0 --> P1 --> P2
p3 --> P0 --> P4 --> P2 --> P1
P3 --> P0 --> P4 --> P1 --> P2
P3 --> P0 --> P2 --> p4 --> P1
P3 --> P0 --> P2 --> P1 --> P4
P3 --> P0 --> P1 --> P4 --> P2
p3 --> P0 --> P1 --> P2 --> P4
it is safe, and it has 8 solutions

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.