Title: Interprocess Communication
1Inter-process Communication
- Operating Systems - Lecture 4
2Inter Process Communication
- Process frequently have to communicate with each
other - Synchronisation
- Example Unix shell commands
- Sharing resources
- Example Writing to a file
3IPC issues
- Processes compete for exclusive access to a
limited number of resources - Reading and writing to a common shared database
- Waiting for an available resource
4Processes compete for exclusive access to a
limited number of resources
5Processes compete for exclusive access to a
limited number of resources
The issue of synchronisation was solved by
Dijkstra in 1965, by the introduction of
SEMAPHORES.
6Processes compete for exclusive access to a
limited number of resources
The issue of synchronisation was solved by
Dijkstra in 1965, by the introduction of
SEMAPHORES. The concept was illustrated by the
model called The Dining Philosophers Problem
7The Dining Philosophers Problem
A table
8The Dining Philosophers Problem
A table
Five diners
9The Dining Philosophers Problem
A table
Five diners
One fork each
10The Dining Philosophers Problem
Each diner needs 2 forks to eat
A table
Five diners
One fork each
11The Dining Philosophers Problem
Each diner needs 2 forks to eat
Each diner either wants to eat or think (sleep)
12The Dining Philosophers Problem
If all 5 diners pick up their left
fork simultaneously...
Each diner needs 2 forks to eat
13The Dining Philosophers Problem
If all 5 diners pick up their left
fork simultaneously...
Each diner needs 2 forks to eat
None will be able to pick up their right
fork. DEADLOCK
14The Dining Philosophers Problem
Attempt to solve the problem...
15The Dining Philosophers Problem
Pick up left fork, then check to see if right
fork is available
Attempt to solve the problem...
16The Dining Philosophers Problem
Pick up left fork, then check to see if right
fork is available
Attempt to solve the problem...
What if they ALL do that simultaneously?
17The Dining Philosophers Problem
A control array of 5 SEMAPHORES Each array
element is either set to EATING or HUNGRY
18The Dining Philosophers Problem
A control array of 5 SEMAPHORES Each array
element is either set to EATING or HUNGRY
EATING
HUNGRY
HUNGRY
EATING
HUNGRY
19The Dining Philosophers Problem
A control array of 5 SEMAPHORES Each array
element is either set to EATING or HUNGRY
EATING
HUNGRY
HUNGRY
A diner can only move into the EATING state if
both neighbours are HUNGRY
EATING
HUNGRY
20The Dining Philosophers Problem
A diner can only move into the EATING state if
both neighbours are HUNGRY HUNGRY diners must
BLOCK if the needed forks are busy.
EATING
HUNGRY
HUNGRY
EATING
HUNGRY
21Readers and Writers
22Readers and Writers
Database
23Readers and Writers
Database
Reader
24Readers and Writers
Database
Reader
Reader
25Readers and Writers
Database
Reader
Reader
Reader
Any number of Readers can access the
database simultaneously
26Readers and Writers
Database
Writer
Reader
Reader
Reader
What if a writer wants to access the database?
27Readers and Writers
Database
Writer
Reader
Reader
Reader
What if a writer wants to access the
database? This is only safe if there are no
readers...
28Readers and Writers
Database
Writer
What if a writer wants to access the
database? This is only safe if there are no
readers...
29Readers and Writers
Database
Access control semaphore
The reading/writing to the database is governed
by an Access control semaphore.
30Readers and Writers
Database
1
Reader
Access control semaphore
The reading/writing to the database is governed
by an Access control semaphore. So long as the
semaphore is not negative, the first reader sets
the semaphore to 1.
31Readers and Writers
Database
2
Reader
Access control semaphore
Reader
A second reader increments the value in the
semaphore
32Readers and Writers
Database
3
Reader
Access control semaphore
Reader
Reader
33Readers and Writers
Database
3
Reader
Access control semaphore
Reader
Writer
Reader
A writer cannot access the database while the
semaphore is positive.
34Readers and Writers
Database
2
Reader
Access control semaphore
Reader
Writer
A writer cannot access the database while the
semaphore is positive.
35Readers and Writers
Database
1
Reader
Access control semaphore
Writer
A writer cannot access the database while the
semaphore is positive.
36Readers and Writers
Database
0
Access control semaphore
Writer
A writer cannot access the database while the
semaphore is positive.
37Readers and Writers
Database
-1
Access control semaphore
The writer forces the semaphore negative
Writer
38Readers and Writers
Database
Reader
-1
Access control semaphore
No readers can access the database while
the semaphore is negative
Writer
39Readers and Writers
Database
0
Access control semaphore
When the writer leaves, the semaphore returns to
zero.
40Readers and Writers
Database
Reader
1
Access control semaphore
When the writer leaves, the semaphore returns to
zero. So Readers or one writer may gain access
41Concurrent Processes
42Overview
- Examples of concurrent programming
- Resource management
- Process synchronisation
- Competing processes
- Semaphore mechanism
- Deadlocks
- Inter-process communications
43Examples of concurrent programming
1. Processes work completely independently
44Examples of concurrent programming
Shared database
2. Processes share common resource
45Examples of concurrent programming
3. Processes work together to form one application
46Resource management
- What resources can processes share?
- CPU, main memory,
- I/O devices
- data items in memory
- messages from one process to another
47Mutual Exclusion
Sharing serially reusable resources. The
problem is ensuring only one process has the
resource at any one time...
48Mutual Exclusion
Sharing serially reusable resources. The
problem is ensuring only one process has the
resource at any one time...
Cant have the resource
Has the resource
49Mutual Exclusion
Sharing serially reusable resources. The
problem is ensuring only one process has the
resource at any one time...
Cant have the resource
Has the resource
50What can go wrong here?
- Imagine two resources X and Y
- and that processes require both X and Y at the
same time - If one process already has X and the other
process already has Y - We get a DEADLOCK
51Deadlock
Resource X
This process has been allocated resource X and is
waiting for resource Y
Resource Y
This process has resource Y and is waiting for
resource X
52 Inter process communication
Waits
Runs
53 Inter process communication
Waits
Runs
Sends a signal to say completion of event Waits
54 Inter process communication
Waits
Runs
Sends a signal to say completion of event Waits
Runs
55Competing processes
Seat on Flight Booking Database
Reads availability
56Competing processes
Seat on Flight Booking Database
Reads availability
Reads availability
57Competing processes
User A
Seat on Flight Booking Database
Reads availability Updates booking system
Reads availability
58Competing processes
User B
Seat on Flight Booking Database
Reads availability Updates booking system
Reads availability Updates booking system
59Competing processes
User B
Seat on Flight Booking Database
Reads availability Updates booking system
Reads availability Updates booking system
Need to synchronise processes so the critical
regions dont run at the same time
60Critical Regions
Process A Process B
Neither process in Critical Region Both
processes can safely run
61Critical Regions
Process A Process B
Process A enters its Critical Region Process B
cannot now enter - and should Wait
62Critical Regions
Process A Process B
Process A exits its Critical Region Process B can
now enter
63How can we control concurrent processes?
64The Open Gate concept
A process asks to see if the Gate is open before
proceeding
65The Open Gate concept
While gate_open FALSE do nothing enddo set
gate_open to FALSE ltcritical region codegt set
gate_open to TRUE
66The Open Gate concept
- Time problems
- There might be a gap between checking the Gate
and setting gate_open to FALSE - There is also a very costly loop while waiting.
67Semaphore mechanism
- A fail-safe system
- With no costly loops.
- Uses Operating System scheduler to make process
WAIT.
68Semaphore mechanism
Semaphore
Shared Resource
69Semaphore mechanism
Semaphore
Shared Resource
70Semaphore mechanism - WAIT
Semaphore
If s gt 0 then s s - 1 else BLOCK process
Shared Resource
71Semaphore mechanism - Signal
Semaphore
If any processes are waiting on s start one of
these processes else ss1
Shared Resource
72Inter-process communications
Synchronisation Message/data exchange