Concurrency Mutual Exclusion and Synchronization
Concurrency Mutual Exclusion and Synchronization
Mutual Exclusion
and
Synchronization
Concurrency – What and Why?
Mutual exclusion- At a time only single process can enter critical section.
Achieve Mutual Exclusion
• Software Solution
o Spinlocks
o Semaphore
o Mutex
Atomic Operations
• Locking Mechanism:
o Acquire: A thread that wants to access the shared resource or critical section
attempts to acquire (lock) the mutex.
o Release: After finishing its work with the shared resource, the thread releases
(unlocks) the mutex, allowing other threads to acquire it.
• Binary Semaphore: mutex can be implemented using a binary semaphore
o When the mutex is free (unlocked), its semaphore value is 1.
o When the mutex is held (locked), its semaphore value is 0.
• The Producer-Consumer Problem -Bounded Buffer Problem
Producer- • a classic synchronization problem in operating systems that
demonstrates the challenges of coordinating concurrent
consumer Problem processes.
• Producers generate data and place it into a buffer.
• Consumers take the data from the buffer and process it.
Producer-consumer Problem
• Buffer:
o A fixed-size storage area that producers fill, and consumers empty.
o The buffer can be thought of as a circular queue.
• Synchronization:
o Mechanisms are needed to ensure that producers do not overwrite data in
the buffer before it is consumed and that consumers do not try to
consume data that has not been produced yet.
Producer-consumer Problem
• Challenges
• Mutual Exclusion:
▪ Ensuring that only one process (either producer or consumer) accesses
the buffer at a time to prevent data corruption.
• Buffer Overflow:
▪ Preventing producers from adding data to a full buffer.
• Buffer Underflow:
▪ Preventing consumers from removing data from an empty buffer.
Solution- Semaphore and Mutex
• Empty: Counts the number of empty slots in the buffer.
• Full: Counts the number of filled slots in the buffer.
• Mutex: Ensures mutual exclusion for buffer access.
Producer Consumer
do { do {
// Produce an item
wait(empty); // Decrease the count of empty slots wait(full); // Decrease the count of full slots
signal(full); // Increase the count of full slots signal(empty); // Increase the count of empty slots
// Consume the item
When a thread enters a monitor to execute a procedure, it acquires the monitor lock.
If another thread tries to enter the monitor while it is locked, it will be blocked until
the monitor becomes available.
Within the monitor, threads can use condition variables to wait for certain conditions
to be met. When a thread calls wait(), it releases the monitor lock and goes to sleep.
When a thread calls signal() or broadcast(), waiting threads are awakened and can
attempt to re-acquire the monitor lock to proceed.
Message Passing
• Message passing - inter-process communication (IPC)
mechanism to allow processes to communicate and
synchronize their actions without sharing the same
memory space.
Message Passing
• Synchronization
o Blocking send, blocking receive
▪ Sender and receiver are blocked until message is delivered.
▪ Sender and receiver are synchronized - each waits for the corresponding
operation to complete before proceeding.
o Non-blocking send, blocking receive
▪ Sender does not wait for the message to be received, continues execution
immediately after sending the message.
▪ The receiver waits for the message to arrive before proceeding.
o Non-blocking send, Non-blocking receive
▪ Both the sender and receiver to continue execution without waiting for the
communication to complete.
Message Passing
• Addressing
o Direct Addressing
o Indirect Addressing
▪ Mailbox- Queues to
hold messages
▪ Decoupling
▪ Producer-consumer
▪ Client-server
Readers-Writer's problem
• Synchronization problem in Operating Systems where multiple processes (or threads)
need to access a shared data area like a file or a database or a block of memory.
• Readers- can only read data
• Writers- can modify data
• Conditions-
• Any number of readers can simultaneously read the file
• Any one writer at a time can write to the file
• If writer is writing , no reader may read it
Readers-Writer's problem- solution
• mutex- Binary Semaphore to
manage Reader's entry
• readcount- counting
semaphore to maitaing
count of readers processes