M 317: Operating Systems: Interprocess Communication
M 317: Operating Systems: Interprocess Communication
M 317: Operating Systems: Interprocess Communication
M 317:
Operating Systems
Chapter 4: IPC
Interprocess Communication
Interprocess communication
Deadlocks
Classic IPC Problems
1
11/5/2021
BUFFER
Progress in time…..
Producer
p1 1 p2 2 p3 3 p4 4
Buffer
3 instead of 2!
Consume
r 1 c1 2 c2
t
2
11/5/2021
A Race Condition
Critical Sections
Critical Section
A section of code in which the process
accesses and modifies shared variables
Mutual Exclusion
A method of preventing for ensuring that one
(or a specified number) of processes are in a
critical section
3
11/5/2021
Rules to Form
Critical Sections
1. No two processes may be simultaneously
inside their CS (mutual exclusion)
2. No assumptions are made about relative
process speeds or number of CPUs
3. A process outside a CS should not block
other processes
4. No process should wait forever before
entering its CS
4
11/5/2021
Definition
Indefinitely delaying the scheduling of a process in favor
of other processes
Cause
Usually a bias in a systems scheduling policies (a bad
scheduling algorithm)
Solution
Implement some form of aging
10
5
11/5/2021
How to Implement
Mutual Exclusion
Three possibilities
Application: programmer builds some method
into the program
Hardware: special h/w instructions provided
to implement ME
OS: provides some services that can be used
by the programmer
All schemes rely on some code for
enter_critical_section, and
exit_critical_section
11
Application
Mutual Exclusion
Application Mutual Exclusion is
implemented by the programmer
hard to get correct, and very inefficient
All rely on some form of busy waiting
(process tests a condition, set a flag, and
loops while the condition remains the
same)
12
6
11/5/2021
Example
Producer
produce
If lock = 1 loop until lock = 0
lock=1
put in buffer
lock=0
Consumer
If lock = 1 loop until lock = 0
lock=1
get from buffer
lock=0
consume
13
Hardware ME :
Test and Set Instruction
Perform an x:=r and r:=1
x is a local variable
r is a global register set to 0 initially
14
7
11/5/2021
Hardware ME :
Exchange Instruction
Exchange: swap the values of x and r
x is a local variable
r is a global register set to 1 initially
x:= 0;
repeat exchange(r, x) until x = 1;
< critical section >
exchange(r, x);
Hardware ME Characteristics
Advantages
can be used by a single or multiple processes
(with shared memory)
simple and therefore easy to verify
can support multiple critical sections
Disadvantages
busy waiting is used
starvation is possible
deadlock is possible (especially with priorities)
16
8
11/5/2021
Another Hardware ME :
Disabling Interrupts
On a single CPU only one process is executed
Concurrency is achieved by interleaving execution
(usually done using interrupts)
If you disable interrupts then you can be sure only
one process will ever execute
One process can lock a system or degrade
performance greatly
17
Mutual Exclusion
Through OS
Semaphores
Message passing
18
9
11/5/2021
Semaphores
A semaphore is
a non-negative integer
that has two valid operations
19
Semaphore Operations
Wait(s)
If s > 0 then s:= s - 1
else block this process
Signal(s)
If there is a blocked process on this
semaphore then wake it up
else s:= s + 1
20
10
11/5/2021
More on Semaphores
21
Produce Wait(mutex)
CS
Get from buffer
Wait(mutex) Signal(mutex)
Put in buffer
Signal(mutex) Consume
11
11/5/2021
Another Example
• Three processes all share a resource on which
– one draws an A
– one draws a B
– one draws a C
• Implement a form of synchronization so that the output appears
ABC
23
Semaphore b = 0, c = 0;
wait(b);
think(); wait(c);
think();
draw_A(); think();
draw_B();
signal(b); draw_C();
signal(c);
24
12
11/5/2021
Bounded-Buffer Problem
We need 3 semaphores:
25
Bounded-Buffer - Semaphores
Shared data
Initially:
26
13
11/5/2021
do {
…
produce an item in nextp
…
wait(empty);
wait(mutex);
…
add nextp to buffer
…
signal(mutex);
signal(full);
} while (1);
27
28
14
11/5/2021
29
What is Deadlock?
Process Deadlock
A process is deadlocked when it is
waiting on an event which will never
happen
System Deadlock
A system is deadlocked when one or
more processes are deadlocked
30
15
11/5/2021
Mutual Exclusion
Shared resources are used in a
mutually exclusive manner
Hold & Wait
Processes hold onto resources they
already have while waiting for the
allocation of other resources
31
16
11/5/2021
No Deadlock Situation
33
34
17
11/5/2021
Deadlock Prevention
Deadlock Detection
Deadlock Avoidance
Deadlock Recovery
35
Deadlock Prevention
Circular Wait
36
18
11/5/2021
Denying the
“Hold & Wait”
Implementation
A process is given its resources on a
"ALL or NONE" basis
Either a process gets ALL its required
resources and proceeds or it gets NONE
of them and waits until it can
37
Denying the
“Hold & Wait”
Advantages
It works
Reasonably easy to code
Problems
Resource wastage
Possibility of starvation
38
19
11/5/2021
Implementation
When a process is refused a resource
request, it MUST release all other
resources it holds
Resources can be removed from a
process before it is finished with them
39
Problems
The cost of removing a process's
resources
The process is likely to lose work it has
done. (How often does this occur?)
Possibility of starvation
40
20
11/5/2021
Implementation
Resources are uniquely numbered
Processes can only request
resources in linear ascending order
Thus preventing the circular wait from
occurring
41
42
21
11/5/2021
Deadlock Avoidance
43
Banker's Algorithm
Definitions
Each process has a LOAN, CLAIM,
MAXIMUM NEED
LOAN: current number of resources held
MAXIMUM NEED: total number resources
needed to complete
CLAIM: = (MAXIMUM - LOAN)
44
22
11/5/2021
Banker’s Problem
45
Assumptions
Establish a LOAN ceiling (MAXIMUM NEED)
for each process
MAXIMUM NEED < total number of resources available
(ie., capital)
Total loans for a process must be less than or
equal to MAXIMUM NEED
Loaned resources must be returned back in
finite time
46
23
11/5/2021
Algorithm
1. Search for a process with a claim that can be
satisfied using the current number of remaining
resources (ie., tentatively grant the claim)
2. If such a process is found then assume that it will
return the loaned resources.
3. Update the number of remaining resources
4. Repeat steps 1-3 for all processes and mark
them
47
Algorithm
Implementation
A resource request is only allowed if it results in a
SAFE state
The system is always maintained in a SAFE state
so eventually all requests will be filled
48
24
11/5/2021
Algorithm
Advantages
Allows jobs to proceed when a prevention algorithm
wouldn't
Problems
Requires there to be a fixed number of resources
What happens if a resource goes down?
Does not allow the process to change its Maximum
need while processing
49
25
11/5/2021
Readers-Writers Problem
Any number of reader activities and writer
activities are running.
At any time, a reader activity may wish to read
data.
At any time, a writer activity may want to
modify the data.
Any number of readers may access the data
simultaneously.
During the time a writer is writing, no other
reader or writer may access the shared data.
51
52
26
11/5/2021
53
54
27
11/5/2021
Readers-Writers problem
1. The first readers-writers problem, requires that
no reader will be kept waiting unless a writer
has obtained access to the shared data.
2. The second readers-writers problem, requires
that once a writer is ready, no new readers may
start reading.
3. In a solution to the first case writers may
starve; In a solution to the second case readers
may starve.
55
56
28
11/5/2021
57
58
29
11/5/2021
59
30
11/5/2021
61
62
31
11/5/2021
Monitors
A new language construct that includes
synchronization
Implemented in Java via synchronized keyword
Only one process can enter a Monitor (use the
entry points) at a time.
For example, compiler might generate a call to a
common semaphore on entry: signal on exit.
If process P makes a monitor call, and process Q
is in the monitor, P will block until Q exits
63
Java Example
class Account {
private double balance;
public Account(double deposit) { }
balance = deposit;
public synchronized double getBalance() {
return balance;
}
public synchronized void deposit(double amount) {
balance += amount;
}
public synchronized void withdraw(double amount) {
balance -= amount;
}
}
64
32
11/5/2021
Equivalence
Monitors and semaphores have equivalent
power
Anything you can do with Monitors, you can do
with semaphores.
Proof: we can implement a monitor with a
semaphore
Anything that you can do with Semaphores,
you can do with Monitors
Proof: we can implement a semaphore with a
monitor
65
Summary
We have seen two problems
Critical Sections - cannot both be modifying
variable
Synchronization - must define ordering
66
33