24 Sync Advanced
24 Sync Advanced
Synchroniza+on:
Advanced
15-‐213:
Introduc0on
to
Computer
Systems
24th
Lecture,
Nov.
18,
2010
Instructors:
Randy
Bryant
and
Dave
O’Hallaron
1
Carnegie Mellon
Today
Producer-‐consumer
problem
Readers-‐writers
problem
Thread
safety
Races
Deadlocks
2
Carnegie Mellon
3
Carnegie Mellon
Producer-‐Consumer
Problem
producer
shared
consumer
thread
buffer
thread
Examples
Mul0media
processing:
Producer
creates
MPEG
video
frames,
consumer
renders
them
Event-‐driven
graphical
user
interfaces
Producer
detects
mouse
clicks,
mouse
movements,
and
keyboard
hits
and
inserts
corresponding
events
in
buffer
Consumer
retrieves
events
from
buffer
and
paints
the
display
4
Carnegie Mellon
exit(0);
}
5
Carnegie Mellon
7
Carnegie Mellon
typedef struct {
int *buf; /* Buffer array */
int n; /* Maximum number of slots */
int front; /* buf[(front+1)%n] is first item */
int rear; /* buf[rear%n] is last item */
sem_t mutex; /* Protects accesses to buf */
sem_t slots; /* Counts available slots */
sem_t items; /* Counts available items */
} sbuf_t;
sbuf.h
8
Carnegie Mellon
/* Clean up buffer sp */
void sbuf_deinit(sbuf_t *sp)
{
Free(sp->buf);
}
sbuf.c
9
Carnegie Mellon
sbuf.c
10
Carnegie Mellon
11
Carnegie Mellon
Today
Producer-‐consumer
problem
Readers-‐writers
problem
Thread
safety
Races
Deadlocks
12
Carnegie Mellon
Readers-‐Writers
Problem
Generaliza+on
of
the
mutual
exclusion
problem
Problem
statement:
Reader
threads
only
read
the
object
Writer
threads
modify
the
object
Writers
must
have
exclusive
access
to
the
object
Unlimited
number
of
readers
can
access
the
object
13
Carnegie Mellon
P(&mutex);
readcnt--;
if (readcnt == 0) /* Last out */
V(&w);
V(&mutex);
}
} 15
Carnegie Mellon
Pool
of
worker
threads
Service
client
Client
Worker
Insert
thread
Accept
Master
descriptors
Remove
Buffer
...
...
connec5ons
thread
descriptors
Worker
Client
thread
Service
client
16
Carnegie Mellon
port = atoi(argv[1]);
sbuf_init(&sbuf, SBUFSIZE);
listenfd = Open_listenfd(port);
while (1) {
connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen);
sbuf_insert(&sbuf, connfd); /* Insert connfd in buffer */
}
}
echoservert_pre.c
17
Carnegie Mellon
18
Carnegie Mellon
echo_cnt.c
19
Carnegie Mellon
Pthread_once(&once, init_echo_cnt);
Rio_readinitb(&rio, connfd);
while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) {
P(&mutex);
byte_cnt += n;
printf("thread %d received %d (%d total) bytes on fd %d\n”,
(int) pthread_self(), n, byte_cnt, connfd);
V(&mutex);
Rio_writen(connfd, buf, n);
}
}
echo_cnt.c
20
Carnegie Mellon
Today
Producer-‐consumer
problem
Readers-‐writers
problem
Thread
safety
Races
Deadlocks
21
Carnegie Mellon
22
Carnegie Mellon
23
Carnegie Mellon
24
Carnegie Mellon
25
Carnegie Mellon
Fix: Modify the func0on so it calls only thread-‐safe func0ons
27
Carnegie Mellon
Reentrant
Func+ons
Def:
A
func+on
is
reentrant
iff
it
accesses
no
shared
variables
when
called
by
mul+ple
threads.
Important
subset
of
thread-‐safe
func0ons.
Require
no
synchroniza0on
opera0ons.
Only
way
to
make
a
Class
2
func0on
thread-‐safe
is
to
make
it
reetnrant
(e.g.,
rand_r
)
All
func+ons
Thread-‐safe
func+ons
Thread-‐unsafe
Reentrant
func+ons
func+ons
28
Carnegie Mellon
29
Carnegie Mellon
Today
Producer-‐consumer
problem
Readers-‐writers
problem
Thread
safety
Races
Deadlocks
30
Carnegie Mellon
/* thread routine */
void *thread(void *vargp) {
int myid = *((int *)vargp);
printf("Hello from thread %d\n", myid);
return NULL;
}
race.c
31
Carnegie Mellon
Race
Elimina+on
Make
sure
don’t
have
unintended
sharing
of
state
/* a threaded program without the race */
int main() {
pthread_t tid[N];
int i;
for (i = 0; i < N; i++) {
int *valp = malloc(sizeof(int));
*valp = i;
Pthread_create(&tid[i], NULL, thread, valp);
}
for (i = 0; i < N; i++)
Pthread_join(tid[i], NULL);
exit(0);
}
/* thread routine */
void *thread(void *vargp) {
int myid = *((int *)vargp);
free(vargp);
printf("Hello from thread %d\n", myid);
return NULL;
} norace.c
32
Carnegie Mellon
Today
Producer-‐consumer
problem
Readers-‐writers
problem
Thread
safety
Races
Deadlocks
33
Carnegie Mellon
Typical
Scenario
Processes
1
and
2
needs
two
resources
(A
and
B)
to
proceed
Process
1
acquires
A,
waits
for
B
Process
2
acquires
B,
waits
for
A
Both
will
wait
forever!
34
Carnegie Mellon
P(s0)
Forbidden
region
P(s1)
for
s1
Thread
1
P(s0)
P(s1)
V(s0)
V(s1)
s0=s1=1
38
Carnegie Mellon
Threads
Summary
Threads
provide
another
mechanism
for
wri+ng
concurrent
programs
Threads
are
growing
in
popularity
Somewhat
cheaper
than
processes
Easy
to
share
data
between
threads
However,
the
ease
of
sharing
has
a
cost:
Easy
to
introduce
subtle
synchroniza0on
errors
Tread
carefully
with
threads!
39