Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following code produces a race condition #define MAX_RESOURCES 5 int availab

ID: 3702916 • Letter: T

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() 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.

Explanation / Answer

Solution:

1)

The race conditions occur in the methods named as decrease_count and increase_count.

2)

The data involved are:

available_resources -= count;

available_resources += count;

3)

Semaphore will be for fixing the race condition:

wait(mutex)

{

while mutex<= 0

}

signal(mutex)

{

mutex= mutex + count;

}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote