Vous êtes sur la page 1sur 3

//Banker's Safety Algorithm #include <stdio.h> #include <stdlib.

h> #define MAX 10 #define true 1 #define false 0 void AllocateResources (int i); void ExecuteProcess (int i); void DeallocateResources (int i); typedef struct ResourceNumbers { int a; int b; int c; }rsop; int NumOfProc; rsop Available; rsop Allocated [MAX]; rsop Need [MAX]; rsop Max [MAX]; int Executed [MAX]; /*Function which asks user to enter resources already allocated as well as the maximum resources a process may need */ inline inputValues (int i) { printf ("\n\nProcess P%d", i); printf ("\n---------------\n"); printf ("\nMax:\n"); printf ("Resource A: "); scanf ("%d", &Max [i].a); printf ("Resource B: "); scanf ("%d", &Max [i].b); printf ("Resource C: "); scanf ("%d", &Max [i].c); printf ("\nAllocated:\n"); printf ("Resource A: "); scanf ("%d", &Allocated [i].a); printf ("Resource B: "); scanf ("%d", &Allocated [i].b); printf ("Resource C: "); scanf ("%d", &Allocated [i].c); } /*Calculates the need of a particular process which is the max-allocated */ inline calculateNeed (int i) { Need [i].a = Max [i].a - Allocated [i].a; Need [i].b = Max [i].b - Allocated [i].b; Need [i].c = Max [i].c - Allocated [i].c; } /*Checks if need of a process is less than the available*/ inline int CanFulfillNeed (int i) { if (((Available.a - Need [i].a) >= 0) && ((Available.b - Need [i].b) >= 0)

&& ((Available.c - Need [i].c) >= 0)) { return true; } return false; } // To print current resource availability inline void printAvailability () { printf ("\nA: %d\t", Available.a); printf ("B: %d\t", Available.b); printf ("C: %d\t", Available.c); } int main (int argc, char **argv, char **env) { int i; int AllExecuted = false; printf ("Number of Processes"); scanf ("%d", &NumOfProc); printf ("Now give the information about the resource requirements of the processes\n"); for (i = 0; i < NumOfProc; i++) { inputValues (i); // Enter need and max for a each process } //Current Availability printf ("What are the available resources with the machine?\n"); printf ("Resource A: "); scanf ("%d", &Available.a); printf ("Resource B: "); scanf ("%d", &Available.b); printf ("Resource C: "); scanf ("%d", &Available.c); //Need calculation for (i = 0; i < NumOfProc; i++) { calculateNeed (i); Executed [i] = false; } while (AllExecuted == false) { AllExecuted = true; // change flag temporarily for (i = 0; i < NumOfProc; i++) { //do for each process //If not executed & need can be fulfilled then add to pr ocess queue if (Executed [i] == false && CanFulfillNeed (i) == true) { AllocateResources (i); ExecuteProcess (i); DeallocateResources (i); printAvailability (); AllExecuted = false; // reassign flag } } }

printf ("\n\nAll processes executed\n"); } /*To allocate resources after a check has been made*/ void AllocateResources (int i) { Allocated [i].a = Max [i].a; Available.a = Available.a - Need [i].a; Allocated [i].b = Max [i].b; Available.b = Available.b - Need [i].b; Allocated [i].c = Max [i].c; Available.c = Available.c - Need [i].c; } //To print the completed process void ExecuteProcess (int i) { printf ("\n\nP%d\t", i); Executed [i] = 1; } //Deallocation at the end of process completion void DeallocateResources (int i) { Available.a += Allocated [i].a; Allocated [i].a = 0; Available.b += Allocated [i].b; Allocated [i].b = 0; Available.c += Allocated [i].c; Allocated [i].c = 0; }

Vous aimerez peut-être aussi