

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


























Threads cooperate in multithreaded programs–
to share resources, access shared data structures
-^
e.g., threads accessing a memory cache in a web server
-^
also, to coordinate their execution
-^
e.g., a disk reader thread hands off blocks to a network writer threadthrough a circular buffer
diskreader
networkwriterthread
thread
circular buffer
3
-^
5
-^
withdraw(account,
amount)
{
int
balance
=
get_balance(account);
b^
l^
t
balance
-=
amount;
put_balance(account,
balance);
return
balance;
}
-^
6
-^
balance
=
get_balance(account);
balance
-=
amount;
t^
t^
it h
balance
=
get_balance(account);
balance
-=
amount;
put_balance(account,
balance);
Execution sequenceas seen by CPU
context switchcontext switch
-^
put_balance(account,
balance);
-^
8
The problem is that two concurrent threads (or processes)access a shared resource (account) without anysynchronization– creates a
race condition
We need mechanisms for controlling access to sharedresources in the face of concurrency
y
Synchronization is necessary for any shared data
-^
Synchronization is necessary for any shared datastructure– buffers, queues, lists, hash tables, scalars, …
9
We want to use mutual exclusion to synchronize access toshared resources
-^
Mutual exclusion makes reasoning about programbehavior easier– making reasoning easier leads to fewer bugs
-^
Code that uses mutual exclusion to synchronize itsexecution is called a critical section– only one thread at a time can execute in the critical
section
all other threads are forced to wait on entry
enter
11
-^
Critical sections have the following requirements– mutual exclusion
prevent thread S from entering the critical section
eventually enter the critical section
f i
?
small with respect to the work being done within itsmall with respect to the work being done within it
12
-^
: a thread calls this before entering a critical section
: a thread calls this after leaving a critical section
release()
:^ a thread calls this after leaving a critical section
-^
acquire()
and
release()
, the thread holds the lock
does not return until the caller holds the lock
-^
at most one thread can hold a lock at a time (usually)
bl
ki
(^
k^
“^
t^
”)
14
int
withdraw(account,
amount)
{
acquire(lock)balance
=
get
balance(account);
acquire(lock);balance
=
get_balance(account);
balance
-=
amount;
b^
l^
(^
b^
l^
)
balance
get_balance(account);
balance
-=
amount;
put
balance(account
balance);
acquire(lock)
itical r
ection
put_balance(account,
balance);
release(lock);return
balance;
}
balance
=
get_balance(account);
balance
-=
amount;
put_balance(account,
balance);
release(lock);
cr
se
put_balance(account,
balance);
release(lock);
-^
-^
is this ok?
15
Problem is that implementation of locks has critical
ti^
t^
sections, too!– the acquire/release must be
atomic
interruptedinterrupted
Need help from the hardware
atomic instr ctions
bl /
bl
i t
t
17
Synchronization can be provided by locks, semaphores,monitors, messages …
-^
Locks are the lowest-level mechanism– very primitive in terms of semantics – error-prone
y p
p
interrupts (also crude, and can only be done in thekernel)
In our next exciting episode …– semaphores are a slightly higher level abstraction
errors
18
-^
Each semaphore has an associated queue of threads
h^
P(
) i
ll d b
th
d
continueif sem was “unavailable” (< 0) place thread on associated
queue; dispatch some other runnable thread
,^
p^
g^
p^
y
increment sem
Semaphores thus have history
20
mutual exclusion; let thread continueth
i^
l^
th
d^
i t d
l^
“^
l”
mutual exclusion; run some other thread
i^
l”
t^
l^
l^
i
(place it on the ready queue)if no threads are on the queue sem is incremented
» the signal is “remembered” for next time P(sem) is called
[th
“V i
” th
d
ti^
ti^
i^
t d]
21