














































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Project; Professor: Chen; Class: Intro Oper System; Subject: Electrical Engineering And Computer Science; University: University of Michigan - Ann Arbor; Term: Unknown 1989;
Typology: Study Guides, Projects, Research
1 / 54
This page cannot be seen from the preview
Don't miss anything!















































EECS 482
1
Motivation
Decompose or separate hard problem into several simpler onesPrograms decompose into several rows (horizontal layers)
main()
{
getInput();computeResult();printOutput(); } getInput() {
cout();cin(); } computeResult() {
sqrt();pow(); } printOutput() {
cout(); }
Processes decompose mix of activities running on a processor
into several parallel tasks (columns)• each job can work independently of the others
Remember, for any area of OS, ask:
main
getInput
cout
job 1
job 2
job 3
EECS 482
2
Definition of a process
code along with all the things the program can read/writenote that process != program
threads
in their own
address space
Play analogyThread
the running computation)
play
Address space
EECS 482
4
Possible to have multiple threads on a computer system that
don’t cooperate or interact at all?• what about multiple programs that are related, e.g. mail
program reads a PDF attachment and starts acroreadprocess to display the attachment?
computer, e.g. running Quake and 482 project at thesame time?
Two possible sources of sharingCorrect example of non-interacting threads
But if threads are cooperating, is it still a helpful abstraction to
think of multiple threads? Or is it simpler to think of a sin-gle thread doing multiple things?
How to build a web server that receives multiple, simultaneous
requests, and that needs to read web pages from disk to sat-isfy each request?
Handle one request at a time
with computation or with network receive
EECS 482
5
Finite-state machine with asynchronous I/Os
request 1 arrivesweb
server receives request 1
web
server starts disk I/O 1a to satisfy request 1
request 2 arrivesweb
server receives request 2
web
server starts disk I/O 2a to satisfy request 2
request 3 arrivesdisk
I/O 1a finishes
have arrived and are being serviced, what disk I/Os areoutstanding and which requests they belong to, and whatdisk I/Os still need to be done to satisfy each request.
Multiple cooperating threads
blocking
disk I/O, wait for I/O to
finish, then continue with next part of its request
progress (and new threads can start to handle newrequests)
Thread system in operating system manages the sharing of the
single CPU among several threads (e.g. allowing onethread to issue a blocking I/O and still allow other threadsto make progress). Applications (or higher-level parts ofthe OS) get a simpler programming model)
Typical domains that use multiple threads
tiple things happening at once.
slow component:
slow component:
slow component
slow component:
EECS 482
7
Arithmetic example
Example
Before we can reason
at all
about parallel processes, we must
know that some operation is
atomic
EECS 482
8
Atomic: indivisible. Either happens in its entirety without
interruption, or has yet to happen at all.• no events from other threads can happen in between the
start and end of an atomic event
In above example, if assignment to x is atomic, then only pos-
sible results are 1 and 2.
In print example above, what are the possible outputs if each
print statement is atomic?
Print example above assumed printing a single character was
atomic. What if printing a single character was
not
atomic?
On most machines, memory load and store are atomicBut many instructions are
not
atomic, e.g. double-precision
floating point on a 32-bit machine (two separate memoryoperations)
If you don’t have any atomic operations, you can’t make one.
Fortunately, the hardware folks give us atomic operations,and we can build up higher-level atomic primitives fromthere
Another example: thread A
thread
B
i=
i=
while (i < 10) {
while
(i
-10)
{
i++
i--
}
}
print “A wins”
“B
wins”
Who will win?Is it guaranteed that someone will win?What if threads run at exactly the same speed and start close
together? Is it guaranteed that it goes on forever?• What if i++ and i-- are not atomic?• Should you worry about this actually happening?
EECS 482
10
Problem definition
most one milk jug
but never more than one person buys milk
Solution #0 (no synchronization)
Peter:
Janet:
if
(noMilk) {
if (noMilk) {
buy
milk
buy milk
}
}
Peter
Janet
3:
look
in fridge (no
milk)
3:
leave
for Kroger
3:
arrive
at Kroger
look in fridge
(no milk)
3:
buy
milk
leave for Kroger
3:
arrive
home, put
arrive at Kroger
in
fridge
3:
buy milk
3:
arrive home, put
milk in fridge.Too much milk!
Mutual exclusion
time (others are excluded). E.g. only 1 person goesshopping at a time.
Critical section
respect to selected other pieces of code.
each other, then multiple threads should not be able tointerleave events from A and B.
because they share data (or other resources, e.g. screen,refrigerator)
(noMilk), buy milk. Peter and Janet’s critical sectionsmust be atomic with respect to each other, i.e. eventsfrom these critical sections must not be interleaved.
EECS 482
11
Peter M. Chen
Assume that the only atomic operations are load and storeIdea: leave note that you’re going to check on the milk status,
so other person doesn’t also buy Peter:
Janet:
if
(noNote) {
if (noNote) {
leave
note
leave note
if
(noMilk) {
if (noMilk) {
buy
milk
buy milk
}
}
remove note
remove note
}
}
Does this work? If not, when could it fail?Is solution #1 better than solution #0?
Idea: change the order of “leave note” and “check note”. This
requires labeled notes (otherwise you’ll see your own noteand think it was the other person’s note)
Peter:
Janet:
leave notePeter
leave
noteJanet
if (no noteJanet)
{
if (no notePeter) {
if (noMilk)
{
if
(noMilk)
{
buy milk
buy
milk
}
}
}
}
remove notePeter
remove
noteJanet
Does this work? If not, when could it fail?
EECS 482
13
Solution: raise the level of abstraction to make life easier for
programmer
A lock prevents another thread from entering a critical section.
e.g. before shopping, leave a note on the fridge, so thatboth Peter and Janet don’t go shopping
Two operations
lock()
: wait until lock is free, then acquire it^ do {
if (lock
is
free)
{
acquire
lock
break
} } while
(1)
unlock()
: release lock
Why was the “note” in Too Much Milk solutions #1 and #2 not
a good lock?
Four elements of locking
All synchronization involves waitingThread can be
running
, or
blocked
(waiting for something)
concurrent programshigh-level synchronization operations provided bysoftware (e.g. locks, semaphores, monitors) low-level atomic operations provided by hardware(e.g. load/store, interrupt enable/disable, test&set)
EECS 482
14
Peter M. Chen
Locks make “Too much milk” really easy to solve!
Peter:
Janet:
lock()
lock()
if
(noMilk) {
if (noMilk) {
buy
milk
buy milk
}
}
unlock()
unlock()
But this prevents Janet from doing stuff while Peter is shop-
ping. I.e. critical section includes the shopping time.
How to minimize the critical section?
enqueue() {
/* find tail
of
queue
*/
for (ptr=head;
ptr->next
!=
NULL;
ptr =
ptr->next);
/* add new element
to
tail
of
queue
*/
ptr->next = new_element;new_element->next
=
NULL;
} dequeue() {
/* if something
on
queue,
then
remove
it
*/
if (head->next
!=
NULL)
{
element =
head->next;
head->next
=
head->next->next;
} return(element);
} What bad things can happen if two threads manipulate queue
at same time?
EECS 482
16
What if you wanted to have dequeue()
wait
if the queue is
empty?
EECS 482
17
Mutual exclusion
threads) is in critical section
Ordering constraints
another thread leaving a critical section)
thing to the queue
Note that this differs from Tanenbaum’s treatment in Section
Monitors use separate mechanisms for the two types of syn-
chronization• use
locks
for mutual exclusion
condition variables
for ordering constraints
A monitor = a lock + the condition variables associated with
that lock
EECS 482
19
So far have described Mesa monitors
other threads
What would be required to ensure that the condition is met
when the waiter returns from wait and starts runningagain?
Hoare monitors give special priority to the woken-up waiter
establish invariant before calling signal())
We’ll stick with Mesa monitors (as do most operating systems)
List the shared data needed to solve the problemDecide which locks (and how many) will protect which data
ent data to be accessed simultaneously, but is more com-plicated
Put lock...unlock calls around the code that uses shared dataList before-after conditions
the shared data that is used to evaluate the condition
Call wait() when thread needs to wait for a condition to be
true; use a while loop to re-check condition after waitreturns
Call signal when a condition changes that another thread might
be interested in
Make sure invariant is established whenever lock is not held
(i.e. before you call unlock, and before you call wait)
EECS 482
20
Problem: producer puts things into a shared buffer, consumer
takes them out. Need synchronization for coordinatingproducer and consumer.• e.g. Unix pipeline (gcc calls cpp | cc1 | cc2 | as)• buffer between producer and consumer allows them to
operate somewhat independently. Otherwise must oper-ate in lockstep (producer puts one thing in buffer, thenconsumer takes it out, then producer adds another, thenconsumer takes it out, etc.)
E.g. coke machine
Variables
can hold “max” cokes)
One lock (cokeLock) to protect this shared data
less concurrency
Ordering constraints
ers are empty (ordering constraint)
ers is completely full (ordering constraint)
consumer
producer