04 - Process Synchronisation Part 1
04 - Process Synchronisation Part 1
Lecture 4
Soumyabrata DEV
https://soumyabrata.dev/
Concurrent Execution
2
Concurrent execution
3
Concurrent execution
Concurrent execution can be achieved via different ways:
4
Concurrent Execution
5
Concurrent Execution Example
sic Example of Concurrent Execution
int A;
Process 1 Process 2
A = 1; A = 2;
if(A == 1) if(A == 2)
printf(“Process 1 wins”); printf(“Process 2 wins”);
7
What about threads?
• If we replace the processes with threads the outcome will be still the
same
8
Atomic Operations
9
Atomic Operations
• It is not possible.
10
Atomic Operations
11
Atomic Instructions
12
Higher-Level Example
Higher-level of Concurrent Execution
Example
int B;
Process 1 Process 2
B = 0; B = 0;
while(B < 10) B++; while(B > −10) B– –;
printf(“Process 1 finished”); printf(“Process 2 finished”);
Ê Variable
Variable B isBshared
is shared
post increments/decrements (“B++”, “B– –”) are atomic
Ê But increments and decrements are atomic
Are there race conditions in this example?
Ê Will the process finish?
will the processes finish?
13
Higher-level
Higher-Level Example
Example of Concurrent Execution
int B;
Process 1 Process 2
B = 0; B = 0;
while(B < 10) B++; while(B > −10) B– –;
printf(“Process 1 finished”); printf(“Process 2 finished”);
Ê The while
Variable sections above are not atomic
B is shared
post increments/decrements (“B++”, “B– –”) are atomic
Ê The process theoretically might never reach
Are there race conditions in this example?
10willorthe-10
processes finish?
14
Higher-level
Higher-Level Example
Example of Concurrent Execution
int B;
Process 1 Process 2
B = 0; B = 0;
while(B < 10) B++; while(B > −10) B– –;
printf(“Process 1 finished”); printf(“Process 2 finished”);
Ê Process synchronisation is all about
Variable B is shared
making high-level constructs
post increments/decrements behave
(“B++”, “B– –”) are atomic
atomically
Are there race conditions in this example?
will the processes finish?
15
Mutual Exclusion & Critical
Section
16
What are these?
Mutual Exclusion and Critical Section are concepts related to
concurrent programming and synchronization in operating systems:
Mutual Exclusion (Mutex):
• Definition: Mutual exclusion is a fundamental concept that ensures
that only one process or thread accesses a shared resource or enters
a critical section at a time, preventing concurrent access by multiple
entities.
Critical Section:
• Definition: A critical section is a portion of code within a program
where shared resources are accessed, and data may be modified. It's
a region of code that should be executed atomically, meaning that no
other process or thread should be able to access it simultaneously.
17
The Milk Problem
• They want to have at most one bottle of milk in the fridge at any time
18
Possible Events
19
The Milk Problem
20
Important Definitions
21
Critical Section
22
Milk Problem
• Mutual exclusion:
• Only let one flatmate do this at a time
23
Locking and Mutual Exclusion
24
Locking Rules
25
Milk Problem Lock
26
Milk Problem (1st Solution Attempt)
if(no_milk) {
if(no_note) {
put note;
buy milk;
remove note;
}
}
27
Milk Problem (1st Solution Attempt)
28
Milk Problem (2nd Attempt)
29
Milk Problem (2nd Attempt)
Process A Process B
if(no_note) { if(note) {
if(no_milk) { if(no_milk) {
buy milk; buy milk;
add note; remove note;
} }
} }
30
Milk Problem (2nd Attempt)
31
Milk Problem (2nd Attempt)
32
Milk Problem (3rd Attempt)
• In order to try to avoid the last issue, let us use two notes (note_A
and note_B) and some basic courtesy protocol
• Each process can now examine each other’s status, but not modify it.
33
Milk Problem (3rd Attempt)
Process A Process B
put note_A; put note_B;
if(no_note_B) { if(no_note_A) {
if(no_milk) if(no_milk)
{ {
buy milk; buy milk;
} }
} }
remove note_A; remove note_B;
34
Milk Problem (3rd Attempt)
35
Milk Problem (4th Attempt)
36
Process A Process B
37
Milk Problem (4th Attempt)
• Is this a solution?
• It was actually the first solution ever given to the mutual exclusion problem
• But it is not satisfactory
38
Issues with the Solution
39
Issues with the Solution
40
Consequence
41
Requirements for True Solution for Critical Section (CS)
1. Mutual exclusion
• One process at most inside the CS at any time
3. Progress
• A process executing outside a CS cannot prevent another process from
entering it
42
Requirements for True Solution for CS
43
Desirable Properties of a Mutual Exclusion (ME) Mechanism
• Simple
• Easy to maintain
• Efficient
• Does not use a lot of resources while waiting
• No busy-waiting
• Scalable:
• It should work when many threads share the critical section
44
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 IPC messages instead
45
Thank you!
46