10 Ipc
10 Ipc
COMMUNICATION
INTERPROCESS COMMUNICATION
8/16/17
4 Dept. of CSE, BUET
SPOOLING EXAMPLE: CORRECT
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
In previous code
for(;;){
int next_free = in;
slot[next_free] = file;
in = next_free+1;
}
What if we use one line of code?
for(;;){
slot[in++] = file
}
8/16/17
7 Dept. of CSE, BUET
WHEN CAN PROCESS BE SWITCHED?
8/16/17
8 Dept. of CSE, BUET
RACE CONDITION
11
SOLUTION REQUIREMENT
12
MUTUAL EXCLUSION WITH BUSY
WAITING
Possible Solutions
Disabling Interrupts
Lock Variables
Strict Alternation
Petersons solution
TSL
8/16/17
13 Dept. of CSE, BUET
DISABLING INTERRUPTS
8/16/17
14 Dept. of CSE, BUET
LOCK VARIABLES
int lock = 0;
while (lock);
lock = 1;
//EnterCriticalSection;
access shared variable;
//LeaveCriticalSection;
lock = 0;
Does the above code work?
8/16/17
15 Dept. of CSE, BUET
STRICT ALTERNATION
16
PROBLEM
17
PETERSON'S SOLUTION
Consists of 2
procedures
enter_region(proc
Each process has to ess#)
call
enter_region with
its own process # leave_region(proc
before entering its ess#)
C.R.
And Leave_region
after leaving C.R.
18
PETERSON'S SOLUTION (FOR 2 PROCESSES)
19
PETERSONS SOLUTION: ANALYSIS(1)
20
PETERSONS SOLUTION: ANALYSIS(2)
21
TSL
Requires hardware support
TSL instruction: test and set lock
Reads content of lock into a Register
Indivisible/Atomic
Stores a nonzero value at lock.
CPU executing TSL locks the memory bus prohibiting
other CPUs from accessing memory
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
http://
research.microsoft.com/en-us/um/people/mbj/Mars_Pathfinder/
Mars_Pathfinder.html
23
SLEEP & WAKEUP
25
SLEEP AND WAKEUP
26
PRODUCER-CONSUMER PROBLEM
SLEEP AND WAKEUP
27
PRODUCER-CONSUMER PROBLEM
SLEEP AND WAKEUP: RACE
CONDITION
Race condition
Unconstrained access to count
CPU is given to P just after C has read count to be 0 but
not yet gone to sleep.
P calls wakeup
Result is lost wake-up signal
Both will sleep forever
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 cant read or
write value, except to set it initially
SEMAPHORES
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.
30
SEMAPHORES
Operations must be atomic
Two downs together cant decrement value
below zero
Similarly, process going to sleep in down wont
miss wakeup from up even if they both
happen at same time
31
SEMAPHORES
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 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
guarrented
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:
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;
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
38
SEMAPHORES: BE CAREFUL
Suppose the following is done in Producers code
Just the
down(&empty) down(&mutex) order is
down(&mutex) down(&empty)
reversed
39
DINING PHILOSOPHERS
Philosophers spend their lives
thinking and eating
Dont interact with their neighbors
When get hungry try to pick up 2
chopsticks (one at a time in either
order) to eat
Need both to eat, then release
both when done
How to program the scenario
avoiding all concurrency
problems?
40
DINING PHILOSOPHERS: A SOLUTION
41
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?
43
ANOTHER ATTEMPT, SUCCESSFUL!
void philosopher(int i) Theoretically solution is OK-
{ no deadlock, no starvation.
while (true)
{
Practically with a
think(); performance bug:
down(&mutex); Only one philosopher can be
take_fork(i); eating at any instant:
take_fork((i+1)%N); absence of parallelism
eat();
put_fork(i);
put_fork((i+1)%N);
up(&mutex);
}
}
44
FINAL SOLUTION PART 1
45
FINAL SOLUTION PART 2
46
THE READERS AND WRITERS PROBLEM
47
A solution to the readers and writers problem
48
ISSUE REGARDING THE SOLUTION
49
THANKS FOR YOUR
PATIENCE