8/8/2019 CS2106 Lec5 IPC II
1/49
Lecture 5
InterprocessCommunication
7 September, 2010
1
8/8/2019 CS2106 Lec5 IPC II
2/49
the
producer-consumer
problem
2
8/8/2019 CS2106 Lec5 IPC II
3/49
producer consumer
3
8/8/2019 CS2106 Lec5 IPC II
4/49
while (1)
if (buffer is empty)
sleepconsume
if (buffer was full)
wake up producer
while (1)
if (buffer is full)
sleep
produce
if (buffer was empty)
wake up consumer
4
8/8/2019 CS2106 Lec5 IPC II
5/49
how to remember
sleep / wake
message?
5
8/8/2019 CS2106 Lec5 IPC II
6/49
the
semaphore
abstraction
6
8/8/2019 CS2106 Lec5 IPC II
7/49
3
semaphore
7
8/8/2019 CS2106 Lec5 IPC II
8/49
down( )
if value is 0 sleep(put in wait list)
value = value - 1
8
8/8/2019 CS2106 Lec5 IPC II
9/49
up( )
value = value + 1 if value is 1
wake someone
9
8/8/2019 CS2106 Lec5 IPC II
10/49
new
ready running
blocked exit
10
8/8/2019 CS2106 Lec5 IPC II
11/49
up( ) and down( )
are atomic
can use enter( ) and leave( ) fromlast lecture to ensure mutual exclusion
11
8/8/2019 CS2106 Lec5 IPC II
12/49
while (1)
if (buffer is empty)
sleepconsume
if (buffer was full)
wake up producer
while (1)
if (buffer is full)
sleep
produce
if (buffer was empty)
wake up consumer
12
8/8/2019 CS2106 Lec5 IPC II
13/49
Process 2::
up(S)
Process 1::
down(S)
semaphore S = 0
13
8/8/2019 CS2106 Lec5 IPC II
14/49
Process 2
: down(S)
:
up(S)
:
semaphore S = 1
Process 1
: down(S)
:
up(S)
:
14
8/8/2019 CS2106 Lec5 IPC II
15/49
semaphore used for:
1. synchronization2. mutual exclusion
15
8/8/2019 CS2106 Lec5 IPC II
16/49
while (1)down(free_slots)produce
up(used_slots)
while (1)
down(used_slots)consume
up(free_slots)
semaphore free_slots = Nsemaphore used_slots = 0
16
8/8/2019 CS2106 Lec5 IPC II
17/49
while (1)down(free_slots)down(mutex)
produceup(mutex)up(used_slots)
while (1)down(used_slots)down(mutex)
consumeup(mutex)up(free_slots)
semaphore free_slots = Nsemaphore used_slots = 0semaphore mutex = 1
17
8/8/2019 CS2106 Lec5 IPC II
18/49
pitfalls ofsemaphore
18
8/8/2019 CS2106 Lec5 IPC II
19/49
Process 2
:down(T)
down(S)
up(S)
up(T)
semaphore S = T = 1
Process 1
:down(S)
down(T)
up(T)
up(S)
19
8/8/2019 CS2106 Lec5 IPC II
20/49
:
down(T)
down(S)
:down(S)
down(T)
20
8/8/2019 CS2106 Lec5 IPC II
21/49
deadlock
21
8/8/2019 CS2106 Lec5 IPC II
22/49
while (1)down(mutex)down(free_slots)
produceup(mutex)up(used_slots)
while (1)down(mutex)down(used_slots)
consumeup(mutex)up(free_slots)
semaphore free_slots = Nsemaphore used_slots = 0semaphore mutex = 1
22
8/8/2019 CS2106 Lec5 IPC II
23/49
while (1)down(mutex)
down(free_slots)
produceup(mutex)
up(used_slots)
while (1)
down(mutex)down(used_slots)consumeup(mutex)
up(free_slots)
23
8/8/2019 CS2106 Lec5 IPC II
24/49
Flickr photo Some rights reserved by fazen
24
http://www.flickr.com/photos/fazen/http://www.flickr.com/photos/fazen/http://creativecommons.org/licenses/by-nd/2.0/http://creativecommons.org/licenses/by-nd/2.0/8/8/2019 CS2106 Lec5 IPC II
25/49
8/8/2019 CS2106 Lec5 IPC II
26/49
26
8/8/2019 CS2106 Lec5 IPC II
27/49
while (1)
thinkpick left chopstick
pick right chopstick eat
put down left chopstickput down right chopstick
27
8/8/2019 CS2106 Lec5 IPC II
28/49
while (1)
thinkwait till left chopstick is available
pick left chopstick
wait till right chopstick is available
pick right chopstick
eatput down left chopstick
put down right chopstick28
8/8/2019 CS2106 Lec5 IPC II
29/49
29
8/8/2019 CS2106 Lec5 IPC II
30/49
starvation
30
8/8/2019 CS2106 Lec5 IPC II
31/49
while (1)
thinkenter( )
pick left chopstick
pick right chopstickeat
put down left chopstickput down right chopstick
leave( )31
8/8/2019 CS2106 Lec5 IPC II
32/49
eat think
hungry
(may block)
32
8/8/2019 CS2106 Lec5 IPC II
33/49
while (1)
think
if aneighbor is eating
wait for chopsticks
eat if a neighbor is waiting and is
ready to eatwake up neighbor
33
8/8/2019 CS2106 Lec5 IPC II
34/49
while (1)
think
state[ i ] = HUNGRYif aneighbor is eating
wait for chopsticks
state[ i ] = EATeat
state[ i ] = THINK
if a neighbor is waitingwake up neighbor
34
8/8/2019 CS2106 Lec5 IPC II
35/49
while (1)
think
state[ i ] = HUNGRY if state[ L ] == EAT || state[ R ] == EAT
down(semaphore[ i ])
state[ i ] = EATeat
state[ i ] = THINK
if state[L] == HUNGRY && state[LL] != EATup(semaphore[ L ])
if state[R] == HUNGRY && state[RR] != EAT
up(semaphore[ R ])
35
8/8/2019 CS2106 Lec5 IPC II
36/49
while (1)thinkstate[ i ] = HUNGRY
if state[ i ] == HUNGRY&&state[ L ] != EAT && state[ R ] != EATup(semaphore[ i ])state[ i ] = EAT
down(semaphore[ i ])
eatstate[ i ] = THINK if state[L] == HUNGRY && state[LL] != EAT && state[ LR ] != EAT
up(semaphore[ L ])
state[ L ] = EATif state[R] == HUNGRY && state[RL] != EAT && state[RR] != EATup(semaphore[ R ])state[ L ] = EAT
36
8/8/2019 CS2106 Lec5 IPC II
37/49
while (1)thinkstate[ i ] = HUNGRY
test( i )
down(semaphore[ i ])eatstate[ i ] = THINK
test( L )
test( R )
37
8/8/2019 CS2106 Lec5 IPC II
38/49
while (1)think
down(mutex)state[ i ] = HUNGRYtest( i )up(mutex)
down(semaphore[ i ])eatdown(mutex)state[ i ] = THINK
test( L )test( R )up(mutex)
38
8/8/2019 CS2106 Lec5 IPC II
39/49
Flickr photo Some rights reserved by Unhindered by Talent
39
http://www.flickr.com/photos/nicmcphee/http://www.flickr.com/photos/nicmcphee/http://creativecommons.org/licenses/by-sa/2.0/http://creativecommons.org/licenses/by-sa/2.0/8/8/2019 CS2106 Lec5 IPC II
40/49
the
mutex
abstraction
40
8/8/2019 CS2106 Lec5 IPC II
41/49
1/0
mutex
41
8/8/2019 CS2106 Lec5 IPC II
42/49
the
condition variable
abstraction
42
8/8/2019 CS2106 Lec5 IPC II
43/49
conditionvariable
43
8/8/2019 CS2106 Lec5 IPC II
44/49
POSIX threads in C
44
8/8/2019 CS2106 Lec5 IPC II
45/49
#include
45
8/8/2019 CS2106 Lec5 IPC II
46/49
gcca.c-lpthread
46
8/8/2019 CS2106 Lec5 IPC II
47/49
pthread_create(..)
pthread_exit(..)pthread_join(..)
pthread_yield(..)
47
8/8/2019 CS2106 Lec5 IPC II
48/49
pthread_mutex_init(..)pthread_mutex_lock(..)
pthread_mutex_unlock(..)pthread_mutex_trylock(..)
pthread_mutex_destroy(..)
48
8/8/2019 CS2106 Lec5 IPC II
49/49
pthread_cond_init(..)pthread_cond_wait(..)
pthread_cond_signal(..)pthread_cond_broadcast(..)
pthread_cond_destroy(..)