Professional Documents
Culture Documents
Chapter 4 - Concurrency-Synchronization Mutual Exclusion, Semaphores and Monitors
Chapter 4 - Concurrency-Synchronization Mutual Exclusion, Semaphores and Monitors
CONCURRENCY-SYNCHRONIZATION:
MUTUAL EXCLUSION, SEMAPHORES AND
MONITORS
CONCURRENCY
• Semaphore
• Monitors
MUTUAL EXCLUSION (MUTEX)
• Mutex lock is essentially a variable that is binary nature that
provides code wise functionality for mutual exclusion. At times,
there maybe multiple threads that may be trying to access same
resource like memory or I/O etc. To make sure that there is no
overriding. Mutex provides a locking mechanism.
• Only one thread at a time can take the ownership of a mutex and
apply the lock. Once it done utilizing the resource and it may release
the mutex lock.
MUTEX
MUTEX
MUTEX
• Can have any value and are not restricted over a certain domain.
• They can be used to control access to a resource that has a limitation
on the number of simultaneous accesses.
• Whenever a process wants to use that resource, it checks if the number
of remaining instances is more than zero, i.e., the process has an
instance available. Then, the process can enter its critical section.
• After the process is over with the use of the instance of the resource, it
can leave the critical section.
For example, let us assume that the
semaphore value is 3.
• Process 1 enters critical section and
semaphore value is changed to 2
• Process 2 also enters critical section and
semaphore value is changed to 1
• Process 2 signals semaphore and
comes out of critical section and
Semaphore value is 2
• Note at this moment only 1 process that
is process 1 is in critical section
• Process 3 and 4 also enter critical
section simultaneously and semaphore
value is 0
• At this moment there are three
processes in Critical section which are
process 1, 3, 4
• Now imagine that process 5 wants to
enter the CS. It would not be able to
enter as semaphore value is 0
• It can only enter once any of the process
1, 3, 4 signals out of the critical section.
Mutex Vs. Semaphores
MUTEX VS. SEMAPHORES
The basic difference between semaphore and mutex is that
semaphore is a signalling mechanism i.e. processes
perform wait() and signal() operation to indicate whether
they are acquiring or releasing the resource.
Mutex is locking mechanism, the process has to acquire
the lock on mutex object if it wants to acquire the resource.
Basis for Comparison Semaphore Mutex
monitor {