Inter-Process Communication
Inter-Process Communication
Inter-Process Communication
1
IPC
2
IPC
3
Inter-thread Communication
4
Race Condition
5
Race Condition
6
Race Condition
7
Race Condition
8
Race Condition
9
Race Condition
10
Critical Regions
11
Critical Regions
12
Critical Regions
13
Mutual Exclusion with Critical
Regions
14
How can we achieve mutual
exclusion?
• Now, let’s examine various proposals to achieve mutual
exclusion
• While one process is busy to update shared memory in its
critical region, no other process will enter its critical
region and cause trouble
15
Disabling Interrupts
16
Lock Variables
17
Lock Variables
18
Strict Alternation
20
Peterson’s Solution
21
Common Problem
22
Different Mechanisms with
Sleep and Wake
• Now, we will see more mechanisms to achieve mutual
exclusion
• These techniques will use Sleep and Wake- two
system calls
• Sleep causes a process to be suspended until another
process wakes it up
• Wake causes a process to wake up
23
The Producer-Consumer
Problem
• When the producer sees a full buffer and goes to
sleep. When the consumer takes out an item, it
awakes the producer
• When the consumer sees an empty buffer and goes to
sleep. When the producer puts an item, it awakes the
consumer
24
The Producer-Consumer
Problem
• We will use count as a variable to stop race conditions
• If the maximum no of information the buffer stores in
N, then producer first checks if count = N. If yes, then
it sleeps; otherwise it will add an item and increment
count by 1
• The consumer tests count. If count = 0, then it sleeps;
otherwise it removes an information and decrements
count by 1
25
The Producer-Consumer
Problem
26
The Producer-Consumer
Problem
• Two processes share a common, fixed-sized buffer
• The producer puts information into the buffer
• The consumer takes it out
Problem arises when-
1. Producer wants to put information into a buffer that
is full
2. Consumer wants to get information from a buffer
that is empty
27
The Producer-Consumer
Problem
• The buffer is empty; the consumer is about to read
count = 0
• Scheduler decides at that very instant to stop
consumer and start producer
• The producer inserts an item and increases count by 1
• The producer will wake the consumer up
• The consumer was not logically sleeping. So, wake
signal is lost.
• The consumer, on its next run, sees count = 0 and
sleeps
28
The Producer-Consumer
Problem
• Soon, the producer fills up the buffer and also goes to sleep
Both will sleep forever
• The main problem here is the lost wake up signal.
• If it were not lost, everything would have worked
• To solve this problem, we can use wake up waiting bit
• When a wake up is sent to a process (producer or
consumer) that is not sleeping, this bit will be set.
• When the sender goes to sleep, it checks this bit
If it is on, then the process will not sleep itself (because
someone MAYBE sleeping)
29
Semaphores
30
Semaphores
31
Producer-Consumer Problem
with Semaphores
32
Producer-Consumer Problem with
Semaphores
• With each semaphore there is an associated waiting queue
• Each entry in a waiting queue has two data items:
– value (of type integer)
– pointer to next record in the list
• Two operations:
– block – place the process invoking the operation on the
appropriate waiting queue
– wakeup – remove one of processes in the waiting queue and
place it in the ready queue
typedef struct{
int value;
struct process *list;
} semaphore;
33
Producer-Consumer Problem with
Semaphores
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
34
Producer-Consumer Problem with
Semaphores
• n buffers, each can hold one item
• Semaphore mutex initialized to the value 1
• Semaphore full initialized to the value 0
• Semaphore empty initialized to the value n
35
Producer-Consumer Problem with
Semaphores
The structure of the producer process
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
36
Producer-Consumer Problem with
Semaphores
The structure of the consumer process
Do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
37
38
Barriers
Some applications are divided into phases
• And have the rule that no process may proceed to the
next phase until all processes are ready to proceed to
the next phase.
• This behaviour maybe achieved by placing a Barrier at
the end of each phase.
When a process reaches the barrier, it is blocked until all
processes reach the barrier.
39
Barriers
Barriers
40
Classical IPC Problems
41
Dining Philosopher Problem
42
Dining Philosopher Problem
43
Dining Philosopher Problem
44
Solutions
45
Solutions
47
Solutions
49
The Readers-Writers Problem
• Dining philosopher problem defines the situation
where processes compete for limited resources
• The Readers-Writers problem defines the situation
where database access is required.
• A reader reads… the writer writes… but the reader is
away and with an old value from the database: simply,
this is the readers-writers problem
50
Solutions
• When a reader comes along, it UPs a semaphore-
means, hey, we are reading, do not disturb
• If a writer comes, then it has to wait.
• If more readers come, they are allowed
51
Solution
• When an active reader is reading, then a writer
comes.
• It sees that a reader is reading, the writer then waits
• If more reader comes, they are queued
• When the active reader finishes, the writer takes place
its schedule
• After finishing of writer, the queued readers are given
chances.
• Concurrency problem and lower performance is key
issue here
52
The Sleeping Barber Problem
• In a barber shop, there is one
barber, some chairs and some
customers
• A barber sleeps if there is no
customer (not even on chairs,
waiting for a haircut )
• A customer wakes up the
barber if it’s his turn to get
his haircut
• A customer waits if there is
any chair left
• A customer leaves, if all the
chairs are occupied 53
Solution
54
Reference
55