Module III Part 3
Module III Part 3
Deadlock, Synchronization,
Inter task Communication
Process Synchronization
• i.e. only one process can execute in its critical section at a time. All
the other processes have to wait to execute in their critical
sections.
Example
Solution to the Critical Section Problem
• Deadlock avoidance
• Deadlock detection
• Deadlock recovery
Deadlock Prevention
Atomic = non-interruptable
wait (S) {
► Synchronization tool that does not require
while S <= 0
busy waiting
; // no-op
► Semaphore S–integer variable
S--;
► Two standard operations modify S:
}
wait()and signal()
signal (S) {
► Less complicated
S++;
► Can only be accessed via two indivisible
}
(atomic) operations
Demonstration of Semaphores
• https://www.youtube.com/watch?v=PQ5aK5wLCQE
Types of Semaphores
• Counting semaphores
• Binary semaphores
Counting Semaphores
• This type of Semaphore uses a count that helps task to be
acquired or released numerous times. If the initial count = 0,
the counting semaphore should be created in the unavailable
state.
Binary Semaphores
• The binary semaphores are quite similar to counting semaphores,
but their value is restricted to 0 and 1. In this type of semaphore,
the wait operation works only if semaphore = 1, and the signal
operation succeeds when semaphore= 0. It is easy to implement
than counting semaphores.
Classical Problems of Synchronization
• Bounded Buffer (Producer-Consumer) Problem
• This problem is generalised in terms of the Producer Consumer problem, where a finite
buffer pool is used to exchange messages between producer and consumer processes.
• Because the buffer pool has a maximum size, this problem is often called the Bounded
buffer problem.
• Solution to this problem is, creating two counting semaphores "full" and "empty" to keep
track of the current number of full and empty buffers respectively.
Contd..
Solution
► One solution of this problem is to use semaphores. The semaphores which will be used
here are:
mutex, a binary semaphore which is used to acquire and release the lock.
empty, a counting semaphore whose initial value is the number of slots in the buffer,
since, initially all slots are empty.
Reader Process
Writer Process
Dinning Philosopher Problem
• At any instant, a philosopher is either eating or thinking. When a
philosopher wants to eat, he uses two chopsticks - one from their
left and one from their right. When a philosopher wants to think, he
keeps down both chopsticks at their original place.
Solution
• A philosopher must be allowed to pick up the chopsticks only if both the
left and right chopsticks are available.
• Allow only four philosophers to sit at the table. That way, if all the four
philosophers pick up four chopsticks, there will be one chopstick left on the
table. So, one philosopher can start eating and eventually, two chopsticks
will be available. In this way, deadlocks can be avoided.
Inter-task Communication
• Process is of two types
• Independent Processes – Processes which do not share data with other processes .
Computation speedup
Modularity
Convenience
Communication Model of ITC
Shared Memory Vs Message Passing
1. Shared Memory
• Ex: Producer Consumer Problem
unbounded-buffer
bounded-buffer
2. Message Passing Method
send(message)
receive(message)
• Physical:
Shared memory
Hardware bus
Network
• Logical:
Direct or indirect
Synchronous or asynchronous
Automatic or explicit buffering
(I) Direct Communication
• Messages are directed and received from mailboxes (also referred to as ports)
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
• Properties of communication link
Link established only if processes share a common mailbox
A link may be associated with many processes
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional
Indirect Communication - Example
Example: Mailbox
(iii) Synchronous Vs Asynchronous
1. Zero capacity – no messages are queued on a link. Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages Sender must wait if link full