3 Ipc
3 Ipc
3 Ipc
Operating Systems
Inter-Process Communications
Dr. Youssef Iraqi
College of Computing
Email: Youssef.Iraqi@um6p.ma
Operating Systems 1
Interprocess Communication
Operating Systems 2
1
2/27/24
Interprocess Communication
Operating Systems 3
Interprocess Communication
Race Conditions
Operating Systems 4
2
2/27/24
Interprocess Communication
Race Conditions
Operating Systems 5
Interprocess Communication
Race Conditions
Operating Systems 6
3
2/27/24
Race Conditions
• Race condition may occur
– two or more processes are reading/writing some shared
data and final result depends on who runs precisely when
Operating Systems 7
Critical Regions
Operating Systems 8
4
2/27/24
Critical Regions
Mutual Exclusion
Approaches for achieving mutual exclusion
– used to solve race conditions
– some do not completely eliminate race conditions
1. Mutual Exclusion with Busy Waiting Methods
•Disabling Interrupts
•Lock variables
•Strict Alternation
•Peterson’s Solution
•Test & Set Lock
2. Sleep and Wakeup
3. Semaphores
4. Message Passing
Operating Systems 10
10
5
2/27/24
11
Operating Systems 12
12
6
2/27/24
Operating Systems 13
13
Operating Systems 14
14
7
2/27/24
Operating Systems 15
15
16
8
2/27/24
– hardware-based solution
– uses a special instruction Test-and-Set-Lock
•reads contents of memory word into register
•then stores a nonzero value at that memory location
Operating Systems 17
17
18
9
2/27/24
Operating Systems 19
19
Operating Systems 20
20
10
2/27/24
Producer-consumer problem
(bounded buffer)
• Two processes share a fixed-size buffer
– producer puts information into buffer
– consumer takes information out of the buffer
Operating Systems 21
21
Producer-consumer problem
(bounded buffer)
• What happens when producer wants to put an item
in buffer but buffer is full?
– producer goes to sleep
– awakened by consumer when consumer removed items
Operating Systems 22
22
11
2/27/24
Producer-
consumer
problem with
fatal race
condition
Operating Systems 23
23
Operating Systems 24
24
12
2/27/24
Semaphores
• Dijkstra (65) recognized problem with previous
example
– WAKEUP is lost because consumer is awake
• use an integer value called semaphore
– count number of wakeups saved for future use
– can have a value of 0 --- no wakeups saved
– can have a positive value --- number of wakeups pending
• two operations: DOWN and UP (P and V originally)
– both are atomic- indivisible operations
– DOWN: if semaphore > 0 then decrement semaphore
else process sleeps; decrement semaphore (when awakened)
– UP: increment semaphore
if any process sleeping then wakeup one process at random
Operating Systems 25
25
Operating Systems 26
26
13
2/27/24
Semaphores
producer-consumer
using semaphores
Operating Systems 27
27
Semaphores
• in solution to producer-consumer problem,
semaphores were used for two distinct purposes
– mutual exclusion
– synchronization
• mutual exclusion (ex: mutex)
– guarantees that only one process at a time will be reading or
writing the buffer and associated variables
• synchronization (ex: empty and full)
– guarantees certain event sequences do or do not occur
– producer stops running when buffer is full
– consumer stops running when buffer is empty
Operating Systems 28
28
14
2/27/24
Mutexes
Operating Systems 29
29
Monitors
Operating Systems 30
30
15
2/27/24
Monitors
Operating Systems 31
31
Message Passing
• semaphores were designed for mutual exclusion and
synchronization in
– single CPU systems
– multi CPU systems with shared memory
• in distributed systems
– multiple CPUs each with private memory
– IPC does not use shared storage
– use of message passing for IPC
32
16
2/27/24
Message Passing
• Design issues:
– Detecting duplicates
– Naming processes
– Authentication
– Performance
Operating Systems 33
33
34
17
2/27/24
Message Passing
The producer-
consumer
problem with
N messages
Operating Systems 35
35
Operating Systems 36
36
18
2/27/24
Operating Systems 37
37
solution to readers
and writers problem
Operating Systems 38
38
19