Question

The following routines are designed to achieve mutual exclusion. Is there any design flaw that might...

The following routines are designed to achieve mutual exclusion. Is there any design flaw that might lead to race condition? If your answer is “NO”, please explain why. If “Yes”, please indicate the flaw. You must justify your conclusion, and a “Yes/No” answer without explanation will receive ZERO credit.  

#define TRUE 1

#define FALSE 0

int mutex;             /* also called lock variable */

BeginRegion()          /* Enter critical section */

{

    while (mutex);     /* do nothing until FALSE */

    mutex = TRUE;

}

EndRegion()            /* Exit critical section */

{

    mutex = FALSE;

}

Homework Answers

Answer #1

YES, there is a slight design flaw in the code.

mutex variable is acting as the control variable, which makes sure whether we enter the critical section of a program or not.

When we call BeginRegion(), we encounter the loop condition, which is dependent on the value of the variable mutex. When the program execution begins, mutex holds some garbage value since we have only declared mutex and not assigned any value to it. So we can't say for sure that the control of the program will enter the loop. This is the only design flaw. When calling the BeginRegion() method for the very first time, the value of mutex variable must be set to 1(TRUE).

Rest of the design is completely fine.

The EndRegion() method is simply responsible for making the value of mutex = 0(FALSE). When mutex is 0, it will get out of the critical section of the program, i.e. get out of the while loop inside the BeginRegion() method.

Even if we consider that we are somehow inside the while loop of BeginRegion() and it is continuously executing.

In order to get out of the loop, the mutex variable must be made equal to 0(FALSE). This is the responsibility of EndRegion() method. Whenever we call the EndRegion() method, we break out of the loop, the value of mutex becomes 1(TRUE) after coming out of the loop. This is correct. Now we can easily call BeginRegion() and EndRegion() as per our convenience.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT