Question
The following program segment is used to manage a finite number of instances of a resource such as a commercial software license. When a user
The following program segment is used to manage a finite number of instances of a resource such as a commercial software license. When a user starts the application, the license count is decremented. When it is terminated, the count is incremented. If all licenses are in use, requests to start the application are denied. There is no queuing of requests. Using a general solution, the maximum and available number of licenses is defined as follows
#define MAX_RESOURCES 5
int available_resources = MAX_RESOURCES;
When a process wishes to obtain a license, 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;
}
The preceding code produces a race condition. Do the following:
Identify the location (or locations) in the code where the race condition occurs. (10 pts)
Identify the data involved in the race condition. (5 pts)
Using a semaphore, fix the race condition. It is OK to modify decrease_count so that the calling process is blocked until sufficient resources are available.
(Please type if possible, I find it hard to understand some of the handwriting on here) Also explination is greatly appreciated
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