written 6.9 years ago by | modified 2.9 years ago by |
Subject: Operating System
Topic: DISTRIBUTED SYSTEMS
Difficulty: Medium
written 6.9 years ago by | modified 2.9 years ago by |
Subject: Operating System
Topic: DISTRIBUTED SYSTEMS
Difficulty: Medium
written 6.7 years ago by |
A mutual exclusion (mutex) is a program object that prevents simultaneous access to a shared resource. This concept is used in concurrent programming with a critical section, a piece of code in which processes or threads access a shared resource.
Only one thread owns the mutex at a time, thus a mutex with a unique name is created when a program starts.
When a thread holds a resource, it has to lock the mutex from other threads to prevent concurrent access of the resource. Upon releasing the resource, the thread unlocks the mutex. Mutex comes into the picture when two threads work on the same data at the same time. It acts as a lock and is the most basic synchronization tool.
When a thread tries to acquire a mutex, it gains the mutex if it is available, otherwise the thread is set to sleep condition. Mutual exclusion reduces latency and busy-waits using queuing and context switches. Mutex can be enforced at both the hardware and software levels.
Disabling interrupts for the smallest number of instructions is the best way to enforce mutex at the kernel level and prevent the corruption of shared data structures. If multiple processors share the same memory, a flag is set to enable and disable the resource acquisition based on availability.
The busy-wait mechanism enforces mutex in the software areas. This is furnished with algorithms such as Dekker's algorithm, the black-white bakery algorithm, Szymanski's algorithm, Peterson's algorithm and Lamport's bakery algorithm. Mutually exclusive readers and read/write mutex class codes can be defined for an efficient implementation of mutex.
Mutual Exclusion Conditions
If we could arrange matters such that no two processes were ever in their critical sections simultaneously, we could avoid race conditions. We need four conditions to hold to have a good solution for the critical section problem (mutual exclusion).
No two processes may at the same moment inside their critical sections.No assumptions are made about relative speeds of processes or number of CPU's. No process should outside its critical section should block other processes.
No process should wait arbitrary long to enter its critical section.