/* 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