



























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!




























Chapter 2
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Ordering matters!
R1 <= xR1 = R1+1R1 => x
R3 <= xR3 = R3+1R3 => x
P
P
x=3 x=
R1 <= x R1 = R1+1R1 => x
R3 <= xR3 = R3+1R3 => x
x=6!
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Use critical regions to provide
mutual exclusion
and help fix race conditions
Four conditions to provide mutual exclusion
No two processes simultaneously in critical region
No assumptions made about speeds or numbers of CPUs
No process running outside its critical region may block another process
No process must wait forever to enter its critical region
Process AProcess B
B blocked
A enters
critical region
B tries to enter
critical region
B enters
critical region
A leaves
critical region
B leaves
critical region
Time
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
#define
#define
#define
// # of processes
int turn;
// Whose turn is it?
int interested[N];
// Set to 1 if process j is interested
void enter_region(int process){
int other = 1-process; // # of the other processinterested[process] = TRUE; // show interestturn = process;
// Set it to my turn
while (turn==process && interested[other]==TRUE)
// Wait while the other process runs
} void leave_region (int process){
interested[process] = FALSE; // I
’m no longer interested
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
<<< is lexicographical order on (ticket#, process ID)
(a,b) <<< (c,d) if (a<c) or ((a==c) and (b<d))
Max(a0,a1,
…
,an-1) is a number k such that k>=ai for all I
choosing initialized to 0
number initialized to 0
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
i
i
Test and set
Swap
Non-concurrent code can leadto unbounded waiting
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
i
Class allows more compleximplementations forsemaphores
Details hidden from processes
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt) class Semaphore {
int value;ProcessList pl;void down ();void up (); };
Semaphore codeSemaphore::down (){
value -= 1;if (value < 0) {
// add this process to plSleep (); } } Semaphore::up () {Process P;
value += 1;if (value <= 0) {
// remove a process P// from plWakeup (P); } }
Implementing semaphores with blocking
Assume two operations:
Sleep(): suspends currentprocess
Wakeup(P): allows process Pto resume execution
Semaphore is a class
Track value of semaphore
Keep a list of processeswaiting for the semaphore
Operations still atomic
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
1 means the semaphore is available
0 means a process has acquired the semaphore
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
One monitor has multiple entry points
Only one process may be in the monitor at any time
Enforces mutual exclusion - less chance for programming errors
Variables belonging to monitor are protected from simultaneousaccess
Procedures in monitor are guaranteed to have mutual exclusion
Language / compiler handles implementation
Can be implemented using semaphores
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
If no process is waiting, signal has no effect
Signals on condition variables aren
’
t
“
saved up
”
Chapter 2
CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Scott A. Brandt)
Problem: P signals on condition variable X, waking Q
Both can
’
t be active in the monitor at the same time
Which one continues first?
Mesa semantics
Signaling process (P) continues first
Q resumes when P leaves the monitor
Seems more logical: why suspend P when it signals?
Hoare semantics
Awakened process (Q) continues first
P resumes when Q leaves the monitor
May be better: condition that Q wanted may no longer hold when P leaves themonitor