![Page 1: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/1.jpg)
1
Interprocess Communication
1. Ways of passing information
2. Guarded critical activities (e.g. updating shared data)
3. Proper sequencing in case of dependencies
2 and 3 apply to threads as well.
![Page 2: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/2.jpg)
2
Race Conditions
Two processes want to access shared memory at the same time.
The final result depends on who runs precisely when.
![Page 3: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/3.jpg)
3
Critical Regions (1)Part of the program where shared memory is accessed.Four conditions to provide correct and efficient
communication:1. Mutual exclusion: No two processes simultaneously in
critical region
2. No assumptions made about speeds or numbers of CPUs
3. Progress: No process running outside its critical region may block another process
4. Fairness: No process must wait forever to enter its critical region (assuming fair scheduling!)
![Page 4: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/4.jpg)
4
Critical Regions (2)
Mutual exclusion using critical regions
![Page 5: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/5.jpg)
5
Attempts for Mutual Exclusion Using Busy Waiting
1. Disabling all interrupts (only by kernel!)2. Lock variables => fatal race condition3. Strict alternation using spin locks - violates
condition 34. Peterson’s solution5. Test-and-set locks (TSL)
Priority inversion problem (H loops while L is in critical section)
![Page 6: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/6.jpg)
6
Strict Alternation
Proposed solution to critical region problem(a) Process 0. (b) Process 1.
![Page 7: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/7.jpg)
7
Peterson’s Solution
Interested(process)=False => process is not in and does not want to enter critical section
If both are interested, a process can enter only if it is the other’s turn.
![Page 8: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/8.jpg)
8
Test-and-set lock
Entering and leaving a critical region using the
TSL instruction
Atomic instruction, implemented in hardware
![Page 9: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/9.jpg)
9
Sleep and Wakeup
Producer-consumer problem with fatal race condition
![Page 10: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/10.jpg)
10
Semaphores
Integer variable with two atomic operations
• down: if 0, then go to sleep;
if >0, then decrement value
• up: increment value and let a sleeping process to perform a down
Implementation by disabling all interrupts by the kernel.
![Page 11: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/11.jpg)
11
Semaphores
The producer-consumer problem using semaphores
![Page 12: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/12.jpg)
12
Mutexes
Implementation of mutex_lock and mutex_unlock
for synchronization of threads in user space
![Page 13: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/13.jpg)
13
Monitors (1)
Example of a monitor - only one process inside the monitor at any time
![Page 14: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/14.jpg)
14
Monitors (2)
• Outline of producer-consumer problem with monitors– only one monitor procedure active at one time– buffer has N slotsCondition variables with wait and signal
![Page 15: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/15.jpg)
15
Message Passing
The producer-consumer problem with N messages
![Page 16: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/16.jpg)
16
Barriers
• Use of a barrier– processes approaching a barrier– all processes but one blocked at barrier– last process arrives, all are let through
![Page 17: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/17.jpg)
17
Dining Philosophers
• Philosophers eat/think• Eating needs 2 forks• Pick one fork at a time • How to prevent deadlock
![Page 18: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/18.jpg)
18
A nonsolution to the dining philosophers problem
![Page 19: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/19.jpg)
19
Deadlock-free code for Dining Philosophers (1)
![Page 20: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/20.jpg)
20
Deadlock-free code for Dining Philosophers (2)
![Page 21: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/21.jpg)
21
The Readers and Writers Problem
![Page 22: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/22.jpg)
22
The Sleeping Barber Problem
![Page 23: 1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of](https://reader035.vdocuments.us/reader035/viewer/2022070305/5515d213550346d46f8b469e/html5/thumbnails/23.jpg)
23
Solution to the Sleeping Barber Problem