IPC-week-4-5-RRR
IPC-week-4-5-RRR
Interprocess Communication
• Consider shell pipeline
– cat chapter1 chapter2 chapter3 | grep tree
– 2 processes
– Information sharing
– Order of execution
Interprocess Communication
• Processes within a system may be independent or
cooperating
• Cooperating process can affect or be affected by
other processes
• Reasons for cooperating processes:
– Information sharing
– Computation speedup
– Modularity
– Convenience
• Cooperating processes require a mechanism to
exchange data and information
IPC issues
1. How one process passes information to another ?
2. How to make sure that two or more processes do not get into
each other’s way when engaging in critical activities?
3. How to do proper sequencing when dependencies
are present?
• Ans 1: easy for threads, for processes different approaches
(e.g., message passing, shared memory)
• Ans 2 and Ans 3: same problems and same solutions apply for
threads and processes
– Mutual exclusion & Synchronization
… out
1 next_free = in;
4 abc
2 Stores F1 into 5 Prog.c
next_free;
6 Prog.n
3 in=next_free+1 in
7 F1
4 next_free = in
F2
5 Stores F2 into
next_free;
… 6 in=next_free+1
… out
1 next_free = in;
4 abc
5 Prog.c 2 next_free = in
/* value: 7 */
3 Stores F1 into 6 Prog.n
next_free; in
7 F1
F2
4 in=next_free+1
5 Stores F2 into
next_free;
… 6 in=next_free+1
11
Solution Requirement
12
Mutual exclusion With Busy Waiting
• Possible Solutions
– Disabling Interrupts
– Lock Variables
– Strict Alternation
– Peterson’s solution
– TSL
17
Problem
• Busy waiting: Continuously testing a variable
until some value appear
– Wastes CPU time
• Violates condition 3
– When one process is much slower than the other
18
Peterson's solution
• Consists of 2 procedures
• Each process has to call
enter_region(process#)
• enter_region with its
own process # before
entering its C.R.
• And Leave_region leave_region(process#)
after leaving C.R.
19
Peterson's solution (for 2 processes)
20
Peterson’s Solution: Analysis(1)
21
Peterson’s Solution: Analysis(2)
22
Busy Waiting: Problems
• Waste CPU time since it sits on a tight loop
• May have unexpected effects:
– Priority Inversion Problem
Example:
• 2 Cooperating Processes: H (high priority ) and L (low
priority )
• Scheduling rule: H is run whenever it is ready
• Let L in C. R. and H is ready and wants to enter C.R.
• Since H is ready it is given the CPU and it starts busy waiting
• L will never gets the chance to leave its C.R.
• H loops forever
23
Sleep & wakeup
• When a process has to wait, change its state to
BLOCKED
• Switched to READY state, when it is OK to retry
entering the critical section
• Sleep is a system call that causes the caller to block
– be suspended until another process wakes it up
• Wakeup system call has one parameter, the process
to be awakened.
• Let’s illustrate the use of sleep & wakeup with an
example: The producer consumer problem
Producer Consumer Problem
• Also called bounded-buffer problem
• Two (or m+n) processes share a common buffer
• One (or m) of them is (are) producer(s): put(s)
information in the buffer
• One (or n) of them is (are) consumer(s): take(s)
information out of the buffer
• Trouble and solution
– Producer wants to put but buffer full- Go to sleep and
wake up when consumer takes one or more
– Consumer wants to take but buffer empty- go to sleep
and wake up when producer puts one or more
25
Sleep and Wakeup
26
Sleep and Wakeup
Producer-consumer problem
27
Sleep and Wakeup: Race condition
28
Semaphores
• A new variable type
• A kind of generalized lock
– First defined by Dijkstra in late 60s
– Main synchronization primitive used in original
UNIX
• Semaphores are like integers, except
– No negative values
– Only operations allowed are up and down – can’t
read or write value, except to set it initially
Semaphores: Types
• Counting semaphore.
– The value can range over an unrestricted domain
• Binary semaphore
– The value can range only between 0 and 1.
– On some systems, binary semaphores are known
as mutex locks as they provide mutual exclusion
Semaphores: Operation
• Operation “down”:
– if value > 0; value-- and then continue.
– if value = 0; process is put to sleep without completing the
down for the moment
• Checking the value, changing it, and possibly going to sleep, is all done as
an atomic action.
• Operation “up”:
– increments the value of the semaphore addressed.
– If one or more process were sleeping on that
semaphore, one of them is chosen by the system (e.g. at
random) and is allowed to complete its down
• The operation of incrementing the semaphore and waking
up one process is also indivisible
– No process ever blocks doing an up.
31
Semaphores: Atomicity
32
Producer & consumer
Semaphores in Producer Consumer
Problem: Analysis
• 3 semaphores are used
– full (initially 0) for counting occupied slots
– Empty (initially N) for counting empty slots
– mutex (initially 1) to make sure that Producer
and Consumer do not access the buffer at
the same time.
• Here 2 uses of semaphores
– Mutual exclusion (mutex)
– Synchronization (full and empty)
• To guarantee that certain event sequences do or do not occur
34
Semaphores: Usage
1. Mutual exclusion
2. Controlling access to limited resource
3. Synchronization
Mutual exclusion
• How to ensure that only one process can enter its C.R.?
• Binary semaphore initialized to 1
• Shared by all collaborating processes
• If each process does a down just before entering CR
and an up just after leaving then mutual exclusion is
guaranteed
do {
down(mutex);
// critical section
up(mutex);
// remainder section
} while (TRUE);
Controlling access to a resource
• What if we want maximum m process/thread can use a
resource simultaneously ?
• Counting semaphore initialized to the number of
available resources
• Semaphore from railway analogy
– Here is a semaphore initialized to 2 for resource control:
Non critical Critical : 2 Cars permitted at a time Non Critical
Value=2
Value=0
Value=1
Synchronization
• How to resolve dependency among processes
• Binary semaphore initialized to 0
• consider 2 concurrently running processes:
– P1 with a statement S1 and
– P2 with a statement S2.
– Suppose we require that S2 be executed only after S1
has completed.
P1 P2
S1; down(synch);
up(synch); S2;
Semaphores: “Be Careful”
• Suppose the following is done in Producer’s code
… … Just the
down(&empty) down(&mutex) order is
down(&mutex) down(&empty)
… …
reversed
41
Monitors
• wait is called on some condition variables:
– Calling process is blocked
– another process that had been previously prohibited from
entering the monitor is allowed to enter now.
• signal is called on some condition variable:
– A process waiting on that CV is given the chance to get up.
– Who should run? Caller or awakened one?
Alternative#1: Let newly awakened process to run
suspending the caller.
Alternative#2: Process doing a signal must exit the
monitor immediately i.e. signal statement may 42
appear only as the final statement in a monitor
Outline of producer-consumer using Monitors
44
45
Problems with monitors and semaphores
46
Message Passing
• solution to the problem of semaphores and
monitors w.r.t distributed systems
• A method of IPC that uses two primitives
– send and receive: system calls.
– send(destination, &message)
– receive(source, &message)
– If no message is available:
• The receiver can block until one arrives
• Return immediately with an error code
47
Message Passing
• Challenges (Study of Computer Networks)
– Messages can be lost by the network.
– Acknowledgement and retransmission issue.
– Process naming.
– Authentication.
– Performance issue.
48
Producer Consumer with Message Passing
Assumptions:
•All messages
are of same size
•Messages sent
but not yet
received are
automatically
buffered
51
Dining Philosophers: Problems with
Previous Solution
• Deadlock may happen
• Does this solution
prevents any such thing
from happening ?
– Everyone takes the left
fork simultaneously
Dining Philosophers: Problems with
Previous Solution
Tentative Solution:
• After taking left fork, check whether right fork is available.
• If not, then return left one, wait for some time and
repeat again.
Problem:
• All of them start and do the algorithm synchronously and
simultaneously:
• STARVATION (A situation in which all the
programs run indefinitely but fail to make any
progress)
• Solution: Random wait; but what if the most unlikely of
same random number happens?
53
Another Attempt, Successful!
void philosopher(int i) • Theoretically solution is
{
while (true)
OK- no deadlock, no
{ starvation.
think(); • Practically with a
down(&mutex);
take_fork(i);
performance bug:
take_fork((i+1)%N); – Only one philosopher
eat(); can be eating at any
put_fork(i); instant: absence of
put_fork((i+1)%N); parallelism
up(&mutex);
}
}
54
Final Solution part 1
55
Final Solution Part 2
56
The Readers and Writers Problem
• Dining Philosopher Problem: Models processes that
are competing for exclusive access to a limited
resource
• Readers Writers Problem: Models access to a
database
Example: An airline reservation system- many
competing process wishing to read and write-
– Multiple readers simultaneously- acceptable
– Multiple writers simultaneously- not acceptable
– Reading, while write is writing- not acceptable
57
The Readers and Writers Problem
• First variation – no reader kept waiting unless
writer has permission to use shared object
• Second variation – once writer is ready, it
performs write ASAP
58
A solution to the readers and writers problem
59
Issue regarding the solution
• Inherent priority to the readers
• Say a new reader arrives every 2 seconds and
each reader takes 5 seconds to do its work.
What will happen to a writer?
Issue regarding second variation
• Writer don’t have to wait for readers that
came along after it
• Less concurrency, lower performance
60
Thanks for your sincerity