Module 21: Problem and Solution
  Lecture 41: Solution to Critical Section Problem
 


Solution to Critical Section Problem

  • Consider two processes P0 and P1.
  • Shared variables (for solution to CSP)
    • int turn; boolean flag[2];
    • flag[i] is true when Piis ready to enter its critical section.

 

while (1) {
flag[i] = TRUE; turn = j;
while (flag[j] && turn == j);

Critical section
flag[i] = FALSE;
Remainder section
}

Synchronization Support in OS/ISA

  • Synchronization code can be written using locks
    while (1) {
    Non critical code
    Acquire Lock
    Critical Section Code
    Release Lock
    }
  • Implementation of Lock require support from the ISA
    • TestAndSet instruction, Swap instruction.