0
2.6kviews
What is mutual exclusion? Explain its significance.
1 Answer
4
71views

Mutual Exclusion: If a process Pi is executing in its critical section, then no other processes can be executing in their critical sections. Mutual exclusion is one of the properties of process synchronization and one of the solutions to the critical-section problem. Process synchronization is a way to coordinate processes that use shared data. In a system, there is a possibility of running multiple processes simultaneously. These multiple processes will have shared data or shared variable. For example, there is a shared variable a=10 and processes P1 and P2 that want to access shared variable at the same time.

  1. at time t=0, P1 increments a by 1 -> a= 11 [a=10 because P1 has not committed(saved) the chages]
  2. at t=1, P2 decrements a by 1 -> a=9 [a=10 because P2 has not committed the changes

    Since P1 has not committed the changes, the value is a=10 and modifies it as a=9 but not yet saved]

  3. at t=3, P1 committed the changes -> a=11
  4. at t=4, P2 committed the changes ->a=10 [since P1 had already committed, a=11; on committing the changes made by P2(decrement) a becomes 10, but P2 assumes a = 9]

According to P1 the final value is a=11, and according to process P2 the final value is a=9, but actually a=10 because P2 committed the changes after P1 had committed the changes.

This results in data inconsistency. The data inconsistency can result in what is called race condition. A race condition occurs when two or more operations are executed at the same time, not scheduled in the proper sequence, and not exited correctly.

To avoid the race condition, each process has to execute a code segment called the critical section. If the process wants to access the shared variable it can only do it in the critical section. In the critical section, the process can update or write the value of the shared variable.

When one process enters the critical section, the process will modify the data, commit the data then comes out of the critical section. As long as one process is in the critical section, no other process may be in its critical section. The property of allowing only one process in the critical section is called Mutual exclusion.

On considering the same above example-

Initially, the value of the shared variable a=10.

  1. Process P1 enters the critical section
  2. increments a by 1
  3. P1 commits its changes, the final value is a=11.
  4. P1 comes out of the critical section.

    [Meanwhile, if P2 tries to enter the critical section when P1 is already inside, P2 checks whether any process applies the lock or not. This specific lock is a mutex lock. Mutex lock is a variable with binary nature where the process inside the critical section applies lock. Once it is done utilizing the shared resources, the process releases the mutex lock.]

  5. As soon as the P1 releases the mutex lock, P2 enters the critical section.

  6. Now, a=11. If P2 wants the value of a to be 9 then it subtracts a by 2

  7. P2 commits the changes, the final value is a=9

  8. P2 releases the lock and comes out of the critical section.

With the mutual exclusion property, the race condition can be avoided.

Please log in to add an answer.