05 - Process Synchronisation Part 2
05 - Process Synchronisation Part 2
Lecture 5
Soumyabrata DEV
https://soumyabrata.dev/
Implementations
1. Semaphores
• Simple, but hard to program with
• Very low level
2. Monitors
• Higher level mechanism
• Requires higher-level programming languages
3. Messages
• Synchronisation without shared memory
• Uses Interprocess Communication (IPC) messages instead
2
Semaphores
3
Semaphore
4
Semaphore
5
Semaphore
6
Binary Semaphore
7
Counting Semaphore
8
Semaphore Operations
9
Semaphore
wait(S){
while(S<=0); // busy waiting
S--;
}
signal(S){
S++;
}
10
The Producer/Consumer Problem
11
The Producer/Consumer Problem
12
Problem Constraints
13
Synchronisation
14
P/C Solution with Semaphores
Initialization of semaphores
mutex = 1
15
P/C Solution with Semaphores
Solution for Producer
do{
16
P/C Solution with Semaphores
Solution for Consumer
do{
wait(full); Explanation:
wait(mutex);
As the consumer is removing an item from
buffer, therefore the value of “full” is
// remove item from reduced by 1 and the value is mutex is also
buffer reduced so that the producer cannot
access the buffer at this moment. Now, the
signal(mutex); consumer has consumed the item, thus
signal(empty); increasing the value of “empty” by 1. The
value of mutex is also increased so that
// consumes item producer can access the buffer now.
}while(true)
17
Monitors
18
The Problem with Semaphores
20
Monitors
21
Example Monitor
monitor example {
int i; (Internal data)
condition c; (Condition variable)
23
Monitor Rules
24
Blocking in Monitors
25
wait(condition)
26
signal(condition)
27
Monitor for Producer/Consumer
28
Monitor for Producer/Consumer
29
Producer and Consumer Code
30
Benefits of Monitors
31
Drawbacks of Monitors
• Monitors are a high-level abstraction and may not be suitable for all
types of concurrent applications, especially in low-level or embedded
systems.
• Proper use of monitors requires understanding and adhering to
monitor-specific programming practices.
32
Messages
33
Messages System
Message Components
34
Messages System
35
Message System
36
Message System Scheme
m Scheme
• Process A sends messages to process B using a mailbox
A B
mailbox
a pipe can be implemented with messages 37
Message System Scheme
38
Message System Operations
39
Message System Addressing
40
Message Format
41
Why Use Messages?
42
Mutual Exclusion with Messages
• Producer/Consumer Constraints
• Execution flow constraints
43
Producer/Consumer Constraints
• receive()
• Calling process waits when the mailbox is empty until a
message is added
• send()
• Calling process waits when mailbox is full until there is
space for another message
44
Execution Flow Constraints
45
Execution Flow Constraints
46
Execution Flow Constraints
• Blocking Receive
• Non-blocking send
47
Mutual Exclusion using Messages
• Whenever they leave the critical section they must send a new
message to the mailbox
48
Producer/Consumer Algorithm Using Messages
49
Producer/Consumer Algorithm Using Messages
50
Thank you!
51