Q171: Solution to Critical Section Problem

Question 171: Does the following solution solve the critical section problem?


while(true) {

while (flag);    // Spin lock

flag = true;

// critical section

flag = false;

// remainder section

}

Options:

  1. No, there should be ‘if’ condition instead of ‘while’.
  2. Yes, it solves the problem.
  3. No, this violates the mutual exclusion requirement as both processes can be in critical region if context switch happens just after ‘while’ loop breaks but before setting flag as true.
  4. No, this violates the mutual exclusion requirement as both processes can be in critical region if context switch happens just before ‘while’ loop breaks and before setting flag as true.

Solution: The correct answer is the third one. Mutual exclusion is not ensured here, since both processes can enter simultaneously into the critical section in the following case: Process 1 checks the flag as false initially and before making it true, a context switch happens and Process 2 also checks the flag as false. Now, both set the flag as true and are in the critical section.

Q135: Critical Section Problem

Question #135: Which of the following is not a requirement for the solution to critical section problem?

Options:

  1. No two processes may be simultaneously inside their critical regions.
  2. No assumptions may be made about speeds or the number of CPUs.
  3. No process running outside its critical region may block other processes.
  4. No process has to wait for more than a certain amount of time.

Solution: The correct answer is the fourth one. The correct requirement is that no process has to wait forever.