Module 2 -Interprocess Communication
Module 2 -Interprocess Communication
condition)
Process 1: a = a + 1; b =
b + 1 Process 2: b = 2 x
b; a = 2 x a Initially a = b
Execution sequence:
a=a+1
b = 2 x
bb =b
+1a=
2xa
At the end a ≠ b.
Critical section (region): Portion of a program that accesses shared
variables
Mutual exclusion: Mechanism which makes sure that two or more
processes do not access a common resource at the same time.
Critical Regions (1)
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Mutual Exclusion with Busy Waiting
• Disabling interrupts
• Lock variables
• Strict alternation
• Peterson's solution
• The TSL instruction
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Disabling Interrupts
A hardware solution:
1. Disable interrupts
2. Enter critical section
3. Do something in critical section
4. Exit critical section
5. Re-enable interrupts
turn:
turn = 0: process 0 can enter critical
section turn = 1: process 1 can enter
critical section
Limitations:
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The TSL Instruction
TSL: combine
(Mem) -> R and 1 -> Mem into an atomic operation.
The TSL Instruction
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Sleep and Wakeup
• Ignore
• Queue
The Producer-Consumer Problem
A circular buffer has n slots. Producer puts an item into buffer each
time. Consumer takes an item out of the buffer each time.
N-1
1
2
The Producer-Consumer Problem
...
The producer-consumer problem with a
fatal race condition.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Producer-Consumer Problem
Problem: wakeup sent to a process that has not gone to
sleep. Example:
• Buffer empty.
• Consumer reads count = 0 and is switched out (not sleep yet).
• Producer enters an item in buffer and increments the counter.
• Producer sends wakeup. Wakeup lost.
• Consumer is scheduled to run again.
• Consumer goes to sleep.
• Producer eventually fills buffer and goes to sleep.
Quick fix:
Set wakeup waiting bit if wakeup is sent to a non-sleeping
process.
If a process tries to go sleep and the bit is on, clears the bit and
stays awake.
More than one wakeup ?
Semaphores
A synchronization integer variable.
Two atomic operations: down and up
Semaphores
Binary semaphore:
Two values 0 and 1, used for mutual exclusion (i.e. to
ensure that only one process is accessing shared
information at a time) semaphore mutex = 1
down(mutex);
critical-
section();
up(mutex);
Counting semaphore:
Used for synchronizing access to a shared resource by several
concurrent processes (i.e. to control how many processes can
concurrently perform operations on the shared resource).
When is a Binary Semaphore useful?
.
The producer-consumer problem using semaphores.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Semaphores
Although semaphores provide a simple and
sufficiently general scheme for IPC, they suffer
from the following drawbacks:
Problem description:
Five philosopher sit around a round table, and each
of them has one fork.
Goal: no starvation.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem
...
Figure 2-46. A solution to the dining philosophers
problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem(contd..)
...
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers
Problem(Contd..)
A solution to the dining philosophers problem.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Readers and Writers Problem
Problem description: