Question
The following code produces a race condition #define MAX_RESOURCES 5 int available_resources = MAX_RESOURCES; When a process wishes to obtain alicense, it invokes the decrease_count()
The following code produces a race condition
#define MAX_RESOURCES 5
int available_resources = MAX_RESOURCES;
When a process wishes to obtain alicense, it invokes the decrease_count() function passing in 1:
// Decrease available_resources by count resources
// return 0 if no resources are available, 1 if successful
int decrease_count(int count) {
if (available_resources >= count) {
available_resources -= count;
return 1;
}
return 0;
}
When a process wants to return a number of resources, it calls increase_count() with the count.
// Increases available_resources by count
void increase_count(int count) {
available_resources += count;
}
1)Identify the locations in the code where the race condition occurs
2)Identify the data involved in the race condition
3)Using a semaphore, fix the race condition. It's okay to modify decrease_count so that the calling process is blocked until sufficient resources are available.
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