Professional Documents
Culture Documents
Lecture 4
Interprocess
Communication
31 August, 2010
2
why IPC?
3
message passing
4
shared memory
5
UNIX IPC
6
signal
7
#include
<signal.h>
:
kill(pid,
sig);
kill(pid,
SIGTERM);
8
at the receiver, default
actions are defined for
each signals
9
but reaction to most
signals can be
customized
10
C
function pointers
11
//
function
taking
in
a
//
char
*
and
returning
an
int.
12
return
var arg
type (* name )( type )
13
function pointers can be:
(i) passed into function,
(ii) returned
(iii) defined as new type
14
#include
<signal.h>
void
*wont_die(int
sig)
{
return;
}
:
void (*prev_handler)(int);
15
pipe
16
#include
<unistd.h>
:
int files[2];
pipe(files);
17
18
Flickr Photo Some rights reserved by Darwin Bell
19
race conditions
20
demo
21
mutual exclusion
23
processes typically alternate
between critical and non-
critical region.
24
while (1)
enter( )
critical_region
leave( )
noncritical_region
25
how to implement
enter( ) and leave( )
26
lock variable
27
enter( )
while (lock);
lock = 1;
leave( )
lock = 0;
28
process A process B
while (lock);
while (lock);
lock = 1;
lock = 1;
29
interrupts
30
enter( )
disable interrupt
leave( )
enable interrupt
31
Petersonʼs
Algorithm
32
process A
enter( )
interested[A] = 1
turn = B
while (turn == B && interested[B]);
leave( )
interested[A] = 0
33
process B
enter( )
interested[B] = 1
turn = A
while (turn == A && interested[A]);
leave( )
interested[B] = 0
34
process A process B
i [A] = 1
t=B
i [B] = 1
t =A
while (t is A && i [A]);
while (t is B && i [B]);
35
process A process B
i [A] = 1
t=B
i [B] = 1
while (t is B
t =A
while (t is A && i [A]);
&& i [B]);
36
atomic instructions
37
TSL R, lock
38
enter( )
TSL
R,
lock
CMP
R,
#0
JNE
enter
RET
leave( )
MOV
lock,
#0
39
test&set(lock)
leave( )
lock = 0;
41
XCHG R, lock
42
enter( )
MOV
R,
#1
XCHG
R,
lock
CMP
R,
#0
JNE
enter
RET
leave( )
MOV
lock,
#0
43
while (1)
enter( )
critical_region
leave( )
noncritical_region
44
producer -
consumer
problem
45
producer consumer
46
while (1) producer
if (buffer is full)
sleep
produce
if (buffer was empty)
wake up consumer
47
while (1) consumer
if (buffer is empty)
sleep
consume
if (buffer was full)
wake up producer
48
while (1)
if (buffer is empty)
while (1)
if (buffer is full)
sleep
produce
if (buffer was empty)
wake up consumer
sleep
consume
if (buffer was full)
wake up producer
49
Flickr Photo Some rights reserved by fofurasfelinas
50