Interprocess Communication - PowerPoint PPT Presentation

1 / 72
About This Presentation
Title:

Interprocess Communication

Description:

Each diner either. wants to eat or 'think' (sleep) The Dining Philosophers Problem ... A diner can only move into the. EATING state if both neighbours are. HUNGRY ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 73
Provided by: robco8
Category:

less

Transcript and Presenter's Notes

Title: Interprocess Communication


1
Inter-process Communication
  • Operating Systems - Lecture 4

2
Inter Process Communication
  • Process frequently have to communicate with each
    other
  • Synchronisation
  • Example Unix shell commands
  • Sharing resources
  • Example Writing to a file

3
IPC 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

4
Processes compete for exclusive access to a
limited number of resources
5
Processes 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.
6
Processes 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
7
The Dining Philosophers Problem
A table
8
The Dining Philosophers Problem
A table
Five diners
9
The Dining Philosophers Problem
A table
Five diners
One fork each
10
The Dining Philosophers Problem
Each diner needs 2 forks to eat
A table
Five diners
One fork each
11
The Dining Philosophers Problem
Each diner needs 2 forks to eat
Each diner either wants to eat or think (sleep)
12
The Dining Philosophers Problem
If all 5 diners pick up their left
fork simultaneously...
Each diner needs 2 forks to eat
13
The 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
14
The Dining Philosophers Problem
Attempt to solve the problem...
15
The Dining Philosophers Problem
Pick up left fork, then check to see if right
fork is available
Attempt to solve the problem...
16
The 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?
17
The Dining Philosophers Problem
A control array of 5 SEMAPHORES Each array
element is either set to EATING or HUNGRY
18
The Dining Philosophers Problem
A control array of 5 SEMAPHORES Each array
element is either set to EATING or HUNGRY
EATING
HUNGRY
HUNGRY
EATING
HUNGRY
19
The 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
20
The 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
21
Readers and Writers
22
Readers and Writers
Database
23
Readers and Writers
Database
Reader
24
Readers and Writers
Database
Reader
Reader
25
Readers and Writers
Database
Reader
Reader
Reader
Any number of Readers can access the
database simultaneously
26
Readers and Writers
Database
Writer
Reader
Reader
Reader
What if a writer wants to access the database?
27
Readers 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...
28
Readers and Writers
Database
Writer
What if a writer wants to access the
database? This is only safe if there are no
readers...
29
Readers and Writers
Database
Access control semaphore
The reading/writing to the database is governed
by an Access control semaphore.
30
Readers 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.
31
Readers and Writers
Database
2
Reader
Access control semaphore
Reader
A second reader increments the value in the
semaphore
32
Readers and Writers
Database
3
Reader
Access control semaphore
Reader
Reader
33
Readers and Writers
Database
3
Reader
Access control semaphore
Reader
Writer
Reader
A writer cannot access the database while the
semaphore is positive.
34
Readers and Writers
Database
2
Reader
Access control semaphore
Reader
Writer
A writer cannot access the database while the
semaphore is positive.
35
Readers and Writers
Database
1
Reader
Access control semaphore
Writer
A writer cannot access the database while the
semaphore is positive.
36
Readers and Writers
Database
0
Access control semaphore
Writer
A writer cannot access the database while the
semaphore is positive.
37
Readers and Writers
Database
-1
Access control semaphore
The writer forces the semaphore negative
Writer
38
Readers and Writers
Database
Reader
-1
Access control semaphore
No readers can access the database while
the semaphore is negative
Writer
39
Readers and Writers
Database
0
Access control semaphore
When the writer leaves, the semaphore returns to
zero.
40
Readers 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
41
Concurrent Processes

42
Overview
  • Examples of concurrent programming
  • Resource management
  • Process synchronisation
  • Competing processes
  • Semaphore mechanism
  • Deadlocks
  • Inter-process communications

43
Examples of concurrent programming
1. Processes work completely independently
44
Examples of concurrent programming
Shared database
2. Processes share common resource
45
Examples of concurrent programming
3. Processes work together to form one application
46
Resource management
  • What resources can processes share?
  • CPU, main memory,
  • I/O devices
  • data items in memory
  • messages from one process to another

47
Mutual Exclusion
Sharing serially reusable resources. The
problem is ensuring only one process has the
resource at any one time...
48
Mutual 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
49
Mutual 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
50
What 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

51
Deadlock
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
55
Competing processes

Seat on Flight Booking Database
Reads availability
56
Competing processes

Seat on Flight Booking Database
Reads availability
Reads availability
57
Competing processes

User A
Seat on Flight Booking Database
Reads availability Updates booking system
Reads availability
58
Competing processes

User B
Seat on Flight Booking Database
Reads availability Updates booking system
Reads availability Updates booking system
59
Competing 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
60
Critical Regions
Process A Process B
Neither process in Critical Region Both
processes can safely run
61
Critical Regions
Process A Process B
Process A enters its Critical Region Process B
cannot now enter - and should Wait
62
Critical Regions
Process A Process B
Process A exits its Critical Region Process B can
now enter
63
How can we control concurrent processes?
64
The Open Gate concept
A process asks to see if the Gate is open before
proceeding
65
The Open Gate concept
While gate_open FALSE do nothing enddo set
gate_open to FALSE ltcritical region codegt set
gate_open to TRUE
66
The 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.

67
Semaphore mechanism
  • A fail-safe system
  • With no costly loops.
  • Uses Operating System scheduler to make process
    WAIT.

68
Semaphore mechanism
Semaphore
Shared Resource
69
Semaphore mechanism
Semaphore
Shared Resource
70
Semaphore mechanism - WAIT
Semaphore
If s gt 0 then s s - 1 else BLOCK process
Shared Resource
71
Semaphore mechanism - Signal
Semaphore
If any processes are waiting on s start one of
these processes else ss1
Shared Resource
72
Inter-process communications
Synchronisation Message/data exchange
Write a Comment
User Comments (0)
About PowerShow.com