#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> // BOOL type int N = 0; // number of processes int M = 0; // number of resources int * available; // resources available vector M int ** Max; // maximum demand * N matrix M int ** allocation; // N * assignment matrix M int ** Need; // needs matrix N * M // initialize data structures void the init (); // data structure destruction void Destory (); // check the security algorithm void security_checks (); int main (int argc, char const * the argv []) { the init (); security_checks () ; Destory (); return 0; } / ** * initialize data structures * / void the init () { the printf ( "input process number and sequence number of types of resources:"); Scanf ( "% D% D", & N, & M); // allocate memory the Available = the malloc (the sizeof (int *) * N); Max = the malloc (the sizeof (int *) * N); Allocation = the malloc (the sizeof ( int *) * N); Need = the malloc (the sizeof (int *) * N); for (int I = 0; I <N; I ++) { Max [I] = the malloc (the sizeof (int) * M); Allocation [I] = the malloc (the sizeof (int) * M); Need [I] = the malloc (the sizeof (int) * M); } // assignment for (int I = 0; I <M; I ++) { the printf ( " the number of input resource type% d ': ", I); Scanf ("% d ", & (the available [I])); } Scanf ("% D% D ", &(Max[i][j]), &(Allocation[i][j])); for (int I = 0; I <N; I ++) { for (int J = 0;j < M; j++) { printf ( "order input process% d for maximum demand and the number of acquired resource type% d of:", i, J); for (int I = 0; I <N; I ++) { Finish [I] = to false; } The Available [J] - = Allocation [I] [J]; Need [I] [J] Max = [I] [J] - Allocation [I] [J]; } } } / ** * destroy data structure * / void Destory () { Free (the Available); for (int I = 0; I <N; I ++) { Free (Max [I]); Free (Need [I]); Free (Allocation [I]); } } / ** * security inspection algorithm * / void security_checks () { int Work [M]; BOOL Finish [N]; for (int I = 0; I <M; I ++) { Work [I] = the Available [I] ; } the number // end of processes; int finish_count = 0 char security_seq [N] [5] ; // security sequence the while (to true) { BOOL = had_allocated to false; // round determines whether to allocate for (int I = 0; I <N; I ++) { iF (Finish [I] == to false) {// find the end of the process is not a BOOL all_valid = to true; for (int J = 0; J <M; J ++) {// if Need for all resources [i] [j] is less than Work [j], is assigned if (Need [i] [j ]> Work [j]) { // whenever are not met, no allocation all_valid = to false; BREAK; } } if (all_valid == true) {// allocate resources and recovered for (int J = 0; J <M; J ++) { Work [J] + = Allocation [I] [J]; } Finish [I] = to true; = to true had_allocated; sprintf (security_seq [finish_count ++], "P% D \ 0", I); } } } IF (finish_count == N) {// end security puts ( "system in a safe state"); the printf ( "process safety sequence is:"); for (int I = 0; I <N; I ++) {// output safety sequence the printf ( "[% S]", security_seq [I]); IF (I = N -! 1) printf ( "=>" ); the puts the else ( ""); } return; } the else IF (== had_allocated to false) {// end unsafe puts ( "system in an unsafe state"); return; } } }