Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include #include #define NUMBER _ OF _ CUSTOMERS 5 #define NUMBER _ OF _ RESOURCES 4 int available [ NUMBER _ OF _ RESOURCES
#include
#include
#include
#define NUMBEROFCUSTOMERS
#define NUMBEROFRESOURCES
int availableNUMBEROFRESOURCES;
int maximumNUMBEROFCUSTOMERSNUMBEROFRESOURCES;
int allocationNUMBEROFCUSTOMERSNUMBEROFRESOURCES;
int needNUMBEROFCUSTOMERSNUMBEROFRESOURCES;
int requestresourcesint customernum, int request;
void releaseresourcesint customernum, int release;
void displaystate;
void initializechar filename
FILE fp fopenfilenamer;
if fp NULL
perrorError opening file";
exitEXITFAILURE;
for int i ; i NUMBEROFRESOURCES; i
fscanffpd &availablei;
for int i ; i NUMBEROFCUSTOMERS; i
for int j ; j NUMBEROFRESOURCES; j
fscanffpd &maximumij;
allocationij;
needij maximumij;
fclosefp;
int requestresourcesint customernum, int request
for int i ; i NUMBEROFRESOURCES; i
if requesti needcustomernumi requesti availablei
return ;
for int i ; i NUMBEROFRESOURCES; i
availablei requesti;
allocationcustomernumi requesti;
needcustomernumi requesti;
return ;
void releaseresourcesint customernum, int release
for int i ; i NUMBEROFRESOURCES; i
availablei releasei;
allocationcustomernumi releasei;
needcustomernumi releasei;
void displaystate
printfCurrent system state:
;
printfAvailable:
;
for int i ; i NUMBEROFRESOURCES; i
printfd availablei;
printf
;
printfMaximum:
;
for int i ; i NUMBEROFCUSTOMERS; i
printfCustomer d: i;
for int j ; j NUMBEROFRESOURCES; j
printfd maximumij;
if j NUMBEROFRESOURCES
printf;
printf
;
printfAllocation:
;
for int i ; i NUMBEROFCUSTOMERS; i
printfCustomer d: i;
for int j ; j NUMBEROFRESOURCES; j
printfd allocationij;
if j NUMBEROFRESOURCES
printf;
printf
;
printfNeed:
;
for int i ; i NUMBEROFCUSTOMERS; i
printfCustomer d: i;
for int j ; j NUMBEROFRESOURCES; j
printfd needij;
if j NUMBEROFRESOURCES
printf;
printf
;
int mainint argc, char argv
if argc
fprintfstderr "Usage: s
argv;
return EXITFAILURE;
initializeargv;
printfWelcome to the Banker's Algorithm Simulation
;
char command;
int customernum, requestNUMBEROFRESOURCES;
while
printfPlease enter commands:
;
printfRQ customernum resource resource resource resource to request resources
;
printfRL customernum resource resource resource resource to release resources
;
printf to display the current values of the data structures
;
printf 'exit' to quit
;
printfCommand: ;
fgetscommand sizeofcommand stdin;
commandstrcspncommand
;
if strcmpcommand "exit"
printfExiting program. Goodbye!
;
break;
else if strcmpcommand
displaystate;
else if strncmpcommandRQ
sscanfcommandRQ d d d d d &customernum, &request &request &request &request;
int result requestresourcescustomernum, request;
if result
printfRequest granted. System is in a safe state.
;
else
printfRequest denied. System would be left in an unsafe state.
;
else if strncmpcommandRL
sscanfcommandRL d d d d d &customernum, &request &request &request &request;
releaseresourcescustomernum, request;
printfResources released successfully.
;
else
printfInvalid command. Please try again.
;
return EXITSUCCESS;
don't know why my code wont run
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started