C language implementation of The Banker algorithm in the operating system
Hey hey, I tried to implement the Banker algorithm in the dormitory tonight.
/*************************************** * ********** Bankers algorithm: the main idea is to increase the size and satisfy the small value first. Author: lybdate: *************************************** * ***********/# include
# Include
# Include
# Include
// Process running status flag # define TRUE1 # define FALSE0 # define WAIT-1/* version 1 # definePMAX20 // assume the maximum number of processes # define RMAX20 // assume the maximum number of resources int Resource [RMAX] = {0 }; // total amount of various types of resources int Max [PMAX] [RMAX] = {0}; // maximum demand of each resource int Need [PMAX] [RMAX] = {0 }; // amount of resources required by each process int Allocation [PMAX] [RMAX] = {0}; // allocated amount of resources int Available [RMAX] = {0 }; // remaining available resources * // version 2 uses an dynamically allocated array. For the convenience of function calling, use the global pointer int * Resource = NULL; // total amount of various types of resources int * Max = NULL; // each resource Int * Need = NULL; // amount of resources required by each process int * Allocation = NULL; // allocated amount of resources int * Available = NULL; // remaining available resources // check whether the system allocation status is secure (core function) int testStatus (const int P, const int R) {int finish = 0; // number of processes completed: int wait = 0; // number of processes waiting: int minR = 0; // minimum resource: int minP = 0; // process for minimum resource requirement int I = 0; int j = 0; int k = 0; int l = 0; int * status = (int *) malloc (P * sizeof (int); // Process status int * Available_tmp = (int *) malloc (R * sizeof (int) ); // Available_tmp is a copy of Available if (status! = NULL & Available_tmp! = NULL) {// All process statuses are set to zero memset (status, FALSE, P * sizeof (int); // copy Availablememcpy (Available_tmp, Available, R * sizeof (int);} else {printf ("pointer NULL \ n"); return FALSE;} while (finish! = P & wait! = P) {// based on the first type of resources, select the minR = Resource [0] process with the minimum Resource demand. // select the maximum value here, to facilitate subsequent comparisons, obtain the minimum minP = 0; for (I = 0; I
Available_tmp [j]) {break;} if (j = R) // can satisfy {// printf ("P % d \ t", minP ); // print the status of the successfully allocated process [minP] = TRUE; finish ++; // If the resource can be allocated, the process can run and stop, and then release the resource, resources need to be recycled here for (l = 0; l
Available [I]) // the requested resources are more than the remaining resources! {Return FALSE;} else {testAllocate [pId * RCount + I] + = reqSource [I] ;}} if (testStatus (PCount, RCount) = TRUE) // security status {// memcpy (Allocation, testAllocate, PCount * RCount * sizeof (int); free (testAllocate); return TRUE ;} else {free (testAllocate); return FALSE ;}// release all Memory Spaces int destroy () {if (Resource = NULL | Max = NULL | Need = NULL | Allocation = NULL | Available = NULL) {return FALSE ;} else {free (Resource); Resource = NULL; free (Max); Max = NULL; free (Need); Need = NULL; free (Allocation); Allocation = NULL; free (Available); Available = NULL; printf ("Destroy \ n"); return TRUE ;}} int main () {int p = 0; // process count int r = 0; // resource category count int reqSource [3] = {0, 3, 4}; readData (& p, & r ); // test now statusif (testStatus (p, r) = TRUE) {printf ("Saft \ n");} else {printf ("nonSaft \ n ");} // for test reqSource [3] = {0, 3, 4}; if (request (p, r, 1, reqSource) = TRUE) {printf ("Allocate \ n") ;}else {printf ("Non-Allocate \ n") ;}// release all memory space destroy (); return 0 ;} /* in.txt 5 3 // number of processes resource types 17 5 20 // total number of various types of resources // maximum demand 5 5 5 95 3 64 0 114 2 54 2 4 // allocated resources count 2 1 24 0 24 0 52 0 43 1 4 // number of remaining resources 2 3 3 */