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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!