










































































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!











































































-^ Created, Runnable, Running, Blocked, Done(Zombie), Done
Problem with cooperating threads:^ •
ABC'' to the terminal, • another process writesCBA'' to the same terminal.Suppose,
initially
Thread
if^ (A
B)^ cout
“they
are equal”;
else “they
are not equal”;
Thread
Can^ you^ predict
what
will
be^ printed
by^ Thread
Can^ you^ state
possible
final
values
of^ A^ and
if^ (savings
savings
=^ savings
Thread
if^ (savings
savings
=^ savings
Computerized
milk
buying:
Too much
milk
problem
Anne
Bob
//^ look
in^ fridge if^ (no
milk)// go^ to^ Kroger Buy^ Milk Put^ milk in^ fridge
//^ look
in^ fridge if^ (no
milk)// go^ to^ Kroger Buy^ Milk Put^ milk in^ fridge
The^ following
execution
is^ possible:
2 revisited:
In the banking example, we want the two lines of code in the threads toform a critical section. Solution to the critical section problem: Locks A^ lock
-^ prevent someone from doing something,e.g., before shopping, leave a note on the fridge
Example
3 revisited: In general, wherever shared variables are manipulated, you need to worryabout using locks to make a set of statements atomic (i.e., turn them into acritical section) How to implement locks?:
Solution
Attempt
Bob
NoteAnne
=^ true; while(NoteBob)
loop;
if^ (no
milk)Buy Milk; NoteAnne
=^ false;
NoteBob
=^ true; while
(NoteAnne)loop; if^ (no
milk)Buy Milk; NoteBob
=^ false;