0% found this document useful (0 votes)
4 views48 pages

Module 2 -Interprocess Communication

The document discusses Inter-process Communication (IPC) and highlights issues such as race conditions and critical sections in concurrent processes. It covers various mechanisms for achieving mutual exclusion, including disabling interrupts, lock variables, strict alternation, Peterson's solution, and the TSL instruction. Additionally, it addresses classical IPC problems like the producer-consumer problem and the readers-writers problem, providing insights into synchronization techniques using semaphores.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
4 views48 pages

Module 2 -Interprocess Communication

The document discusses Inter-process Communication (IPC) and highlights issues such as race conditions and critical sections in concurrent processes. It covers various mechanisms for achieving mutual exclusion, including disabling interrupts, lock variables, strict alternation, Peterson's solution, and the TSL instruction. Additionally, it addresses classical IPC problems like the producer-consumer problem and the readers-writers problem, providing insights into synchronization techniques using semaphores.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 48

Inter-process Communication (IPC)

Processes communicate through shared

memory Spooling: Simultaneous Peripheral

Operation On Line Possible problems (race

condition)

Example: a print spooler.


• To print a file, a process enters the file name in a
designated Spooler directory (an array implemented with
circular queue).
• Another process, printer daemon, prints the files and
removes them from the directory.
• Shared variables: in, out.
• Print procedure:
1. in ->next-free-slot (local variable)
2. Put the file name to print in the array location indexed by
next-free- slot
3. Increment next-free-slot
4. next-free-slot ->in.
Race Conditions

Figure 2-21. Two processes want to


access shared memory at the
same time.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Race Conditions
Assume two processes A & B and processes can be switched
out during execution.
A sequence of actions which can cause problems:

1. Process A: in(= 7) -> next-free-slot


2.A is switched out and B is running.
3. Process B: in(=7) -> next-free-slot
4. B puts fie name to print in Slot 7.
5. B increments its local variable to 8.
6. B stores 8 into in.
7.A is scheduled to run again.
8.A puts file name to print into Slot 7.
9.A increments its local variable to 8.
10.A stores 8 into in.

Race condition: Several processes access and manipulate the


same data concurrently, and the outcome of the execution
depends on the particular access order.
Race Conditions
Another
example:
Bookkeeping application. Need to maintain data coherence,
i.e. keep a = b.

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)

Four conditions required to avoid race


condition:

• No two processes may be simultaneously


inside their critical regions.
• No assumptions may be made about speeds
or the number of CPUs.
• No process running outside its critical
region may block other processes.
• No process should have to wait forever to
enter its critical region.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Critical Regions (2)

Figure 2-22. Mutual exclusion using critical regions.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Mutual Exclusion with Busy Waiting

Proposals for achieving mutual exclusion:

• 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

Give too much power to user


processes. Only works for single CPU.
Lock Variables
A binary shared variable
lock. lock = 1: critical
region occupied
lock = 0: critical region unoccupied

The code for entering critical section:


1. loop: if lock == 1 then goto loop;
2. lock = 1;
3. critical-section();
4. lock =0;

A possible execution sequence:

1. Process A executes (1) and finds lock = 0. Drops from loop.


2. Process A is switched out.
3. Process B checks lock and sees lock = 0 and drops from loop.
4. Process B sets lock = 1 and enters critical section.
5. Process A wakes up, sets lock = 1 (again) and enters critical section.
Strict Alternation

Processes take turns to enter critical

section For two processes, use a variable

turn:
turn = 0: process 0 can enter critical
section turn = 1: process 1 can enter
critical section

Limitations:

1. The faster process has to adapt to the pace


of the slower process
2. Two processes have to take turns to enter their
critical section. No one can enter twice in a row.
Strict Alternation

A proposed solution to the critical region problem.


(a) Process 0. (b) Process 1. In both cases, be sure to
note the semicolons terminating the while
statements.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Peterson's Solution
Combine lock and take turns

Four possibilities for condition:


(turn=process &&
interested[other]=true) from the
point of view of process 0.
Case 1: turn = 0, interested[1] =
false Process 1 is not in critical
region.
Process 0 enters critical region.
Case 2: turn = 0, interested[1]
= true Process 1 is in critical
region.
Process 0 waits.
Case 3: turn = 1, interested[1]=false
Impossible.
Case 4: turn = 1, interested[1]=true
Process 1 is trying to enter critical region, but process 0's
turn first. Process 0 enters critical region.
Peterson's Solution

Peterson’s solution for achieving mutual exclusion.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The TSL Instruction

Need hardware support (machine must have this


special instruction)

TSL: combine
(Mem) -> R and 1 -> Mem into an atomic operation.
The TSL Instruction

Entering and leaving a critical region using


the TSL instruction.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Sleep and Wakeup

Avoid busy waiting. Use sleep and wakeup.

Sleep: a system call that causes the caller to block


until another process wakes it up.

Wakeup(p): wakeup process p.

How to handle wakeup if sent to a process not asleep:

• 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.

Use sleep and wakeup to write procedures for producer and


consumer.

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

Two types of 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?

Binary Semaphores are useful when you need to coordinate


between multiple threads to control access to a shared resource.
They are often used to signal whether a resource is available for use
or to manage access to a critical section.

What is the purpose of a Mutex?

A Mutex is used to provide mutual exclusion, ensuring that only one


thread can enter a critical section of code at a time. This prevents
conflicts and data inconsistencies that can occur when multiple
threads access shared resources concurrently.
Semaphores

Example: Solving the producer consumer problem by


semaphores
Semaphores

.
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:

1.A process that uses a semaphore has to know


WHICH other processes use these semaphore.
May also have to know HOW
these processes are using the semaphore.
2. Semaphore operations must be carefully
installed in a process. The OMISSION of an up or
down may result inconsistencies or deadlocks.
3. Programs using semaphores can be extremely
hard to verify for correctness.
Classical IPC Problems
The dining philosophers problem

Problem description:
Five philosopher sit around a round table, and each
of them has one fork.

Activities: eating and


thinking. To eat, need two
adjacent forks.

Goal: no starvation.

Useful for modeling processes that are competing


for exclusive access to a limited number of
resources, such as tape drive or other I/O devices.
Dining Philosophers Problem

Lunch time in the Philosophy Department.

Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
Dining Philosophers Problem

Figure 2-45. A nonsolution 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

...
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..)

...

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..)
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:

A data area (file or memory) shared among a number of


processes. Some processes (readers) only read the data area.
Other processes (writers) only write to the data area.

Conditions must be satisfied:


1.Any number of readers may simultaneously read the data area.
2. Only one writer at a time may write to the data area.
3. If a writer is writing to the data area, no readers may read it.
The Readers and Writers Problem

Special type of mutual exclusion problem. A special


solution can do better than a general solution.
• Solution one:
Readers have priority. Unless a writer is currently
writing, readers can always read the data.
• Solution two:
Writers have priority. Guarantee no new readers are
allowed when a writer wants to write.
• Other possible solutions:
Weak reader's priority or weak writer's priority.
Weak reader's priority: An arriving reader still has
priority over waiting writers. However, when a
writer departs, both waiting readers and waiting
writers have equal priority.
The Readers and Writers Problem

A solution to the readers and writers problem.


Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved. 0-13-6006639
The Readers and Writers
Problem(contd..)

You might also like