Answered step by step
Verified Expert Solution
Link Copied!

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 NUMBER_OF_CUSTOMERS 5
#define NUMBER_OF_RESOURCES 4
int available[NUMBER_OF_RESOURCES];
int maximum[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
int allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
int need[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
int request_resources(int customer_num, int request[]);
void release_resources(int customer_num, int release[]);
void display_state();
void initialize(char *filename){
FILE *fp = fopen(filename,"r");
if (fp == NULL){
perror("Error opening file");
exit(EXIT_FAILURE);
}
for (int i =0; i NUMBER_OF_RESOURCES; i++){
fscanf(fp,"%d,", &available[i]);
}
for (int i =0; i NUMBER_OF_CUSTOMERS; i++){
for (int j =0; j NUMBER_OF_RESOURCES; j++){
fscanf(fp,"%d,", &maximum[i][j]);
allocation[i][j]=0;
need[i][j]= maximum[i][j];
}
}
fclose(fp);
}
int request_resources(int customer_num, int request[]){
for (int i =0; i NUMBER_OF_RESOURCES; i++){
if (request[i]> need[customer_num][i]|| request[i]> available[i]){
return -1;
}
}
for (int i =0; i NUMBER_OF_RESOURCES; i++){
available[i]-= request[i];
allocation[customer_num][i]+= request[i];
need[customer_num][i]-= request[i];
}
return 0;
}
void release_resources(int customer_num, int release[]){
for (int i =0; i NUMBER_OF_RESOURCES; i++){
available[i]+= release[i];
allocation[customer_num][i]-= release[i];
need[customer_num][i]+= release[i];
}
}
void display_state(){
printf("Current system state:
");
printf("Available:
");
for (int i =0; i NUMBER_OF_RESOURCES; i++){
printf("%d ", available[i]);
}
printf("
");
printf("Maximum:
");
for (int i =0; i NUMBER_OF_CUSTOMERS; i++){
printf("Customer %d: [", i);
for (int j =0; j NUMBER_OF_RESOURCES; j++){
printf("%d", maximum[i][j]);
if (j NUMBER_OF_RESOURCES -1){
printf(",");
}
}
printf("]
");
}
printf("Allocation:
");
for (int i =0; i NUMBER_OF_CUSTOMERS; i++){
printf("Customer %d: [", i);
for (int j =0; j NUMBER_OF_RESOURCES; j++){
printf("%d", allocation[i][j]);
if (j NUMBER_OF_RESOURCES -1){
printf(",");
}
}
printf("]
");
}
printf("Need:
");
for (int i =0; i NUMBER_OF_CUSTOMERS; i++){
printf("Customer %d: [", i);
for (int j =0; j NUMBER_OF_RESOURCES; j++){
printf("%d", need[i][j]);
if (j NUMBER_OF_RESOURCES -1){
printf(",");
}
}
printf("]
");
}
}
int main(int argc, char *argv[]){
if (argc !=2){
fprintf(stderr, "Usage: %s
", argv[0]);
return EXIT_FAILURE;
}
initialize(argv[1]);
printf("Welcome to the Banker's Algorithm Simulation
");
char command[100];
int customer_num, request[NUMBER_OF_RESOURCES];
while (1){
printf("Please enter commands:
");
printf("-'RQ customer_num resource_1 resource_2 resource_3 resource_4' to request resources
");
printf("-'RL customer_num resource_1 resource_2 resource_3 resource_4' to release resources
");
printf("-'*' to display the current values of the data structures
");
printf("- 'exit' to quit
");
printf("Command: ");
fgets(command, sizeof(command), stdin);
command[strcspn(command,"
")]=0;
if (strcmp(command, "exit")==0){
printf("Exiting program. Goodbye!
");
break;
} else if (strcmp(command,"*")==0){
display_state();
} else if (strncmp(command,"RQ",2)==0){
sscanf(command,"RQ %d %d %d %d %d", &customer_num, &request[0], &request[1], &request[2], &request[3]);
int result = request_resources(customer_num, request);
if (result ==0){
printf("Request granted. System is in a safe state.
");
} else {
printf("Request denied. System would be left in an unsafe state.
");
}
} else if (strncmp(command,"RL",2)==0){
sscanf(command,"RL %d %d %d %d %d", &customer_num, &request[0], &request[1], &request[2], &request[3]);
release_resources(customer_num, request);
printf("Resources released successfully.
");
} else {
printf("Invalid command. Please try again.
");
}
}
return EXIT_SUCCESS;
}
don't know why my code wont run
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions