Docsity

Mutual Exclusion Algorithms: Pre- and Post-Protocols - Prof. Bruce W. Char, Study notes of Computer Science

Toggle sidebar
bg1
1
page 1
CS 361 Concurrent programming
Drexel University
Fall 2004
Lecture 7
©Bruce Char and Vera Zaychik. All rights reserved by the author.
Permission is given to students enrolled in CS361 Fall 2004to
reproduce these notes for their own use.
page 2
Pre- and post- CS protocols
• WantToEnterCS(….)
– Serves as a gatekeeper – only lets one thread through at
a time. While the first thread through the gate is in the
critical section, all the other threads executing
WantToEnterCS are prevented from leaving it (the gate
is closed).
• FinishedWithCS(…)
– Notifies the gatekeeper that the thread in the CS is
done; will now let another thread executing
WantToEnterCS through.
page 3
Two aspects of the mutual
exclusion problem
• Implementing the pre- and post- protocols
– Correct iron-clad mutual exclusion (at most one
thread in a critical section at a time)
– Works with any number of threads (1, 2, or
more)
• Using the pre- and post-protocols to
correctly provide mutual exclusion where
they are needed
page 4
Other requirements for a mutual
exclusion algorithm
• No deadlock waiting to get into CS
• Avoidance of starvation in the absence of
contention (one thread trying, never gets
in).
page 5
Other requirements (not always
insisted upon)
• Avoidance of starvation in the presence of
contention
– Multiple threads trying to get into CS, one thread never
gets in
• No livelock: threads take an excessive amount of
time fighting through congestion to get in
• Fairness: no threads favored repeatedly if there is
contention
page 6
Software providing mutual
exclusion
• In the book’s discussion of solutions to the mutual
exclusion problem, Hartley assumes
– Standard load/store register architecture
– Multiply executing concurrent threads that share data
– Single or multiple CPUs that may be identical or
different in speeds
– Access to shared variables can be interleaved if two
threads get into their critical sections in time.
pf3
pf4
pf5

Partial preview of the text

Download Mutual Exclusion Algorithms: Pre- and Post-Protocols - Prof. Bruce W. Char and more Study notes Computer Science in PDF only on Docsity!

page 1

CS 361 Concurrent programming

Drexel University

Fall 2004

Lecture 7

©Bruce Char and Vera Zaychik. All rights reserved by the author. Permission is given to students enrolled in CS361 Fall 2004 to reproduce these notes for their own use.

page 2

Pre- and post- CS protocols

  • WantToEnterCS(….)
    • Serves as a gatekeeper – only lets one thread through at a time. While the first thread through the gate is in the critical section, all the other threads executing WantToEnterCS are prevented from leaving it (the gate is closed).
  • FinishedWithCS(…)
    • Notifies the gatekeeper that the thread in the CS is done; will now let another thread executing WantToEnterCS through.

page 3

Two aspects of the mutual

exclusion problem

  • Implementing the pre- and post- protocols
    • Correct iron-clad mutual exclusion (at most one thread in a critical section at a time)
    • Works with any number of threads (1, 2, or more)
  • Using the pre- and post-protocols to

correctly provide mutual exclusion where

they are needed

page 4

Other requirements for a mutual

exclusion algorithm

  • No deadlock waiting to get into CS
  • Avoidance of starvation in the absence of

contention (one thread trying, never gets

in).

Other requirements (not always

insisted upon)

  • Avoidance of starvation in the presence of contention - Multiple threads trying to get into CS, one thread never gets in
  • No livelock: threads take an excessive amount of time fighting through congestion to get in
  • Fairness: no threads favored repeatedly if there is contention

Software providing mutual

exclusion

  • In the book’s discussion of solutions to the mutual exclusion problem, Hartley assumes - Standard load/store register architecture - Multiply executing concurrent threads that share data - Single or multiple CPUs that may be identical or different in speeds - Access to shared variables can be interleaved if two threads get into their critical sections in time.

page 7

More assumptions

  • Threads do not halt or crash in their pre/post

protocols, or in their critical sections, but

may halt or crash outside of critical

sections.

page 8

Hardware support for mutual

exclusion

  • Definition of an “atomic instruction” – a

machine language instruction that is

executed completely without being

interruptible

  • no interleaving of other instructions from another thread
  • no context switching
  • no hardware interrupts.

page 9

Software solutions for mutual

exclusion: attempt

  • Use “busy waiting”. boolean lockFlag = false; WantToEnterCS(int I) { // thread #I wants to enter while(lockFlag) {}; // busy wait // loops until lockFlag is false; // hopefully other thread will set this false. lockFlag=true; } finishedInCS(int I) { lockFlag = false; }

–What’s wrong with this? page 10

Problem: we can have a race

condition

  • ThreadA: load lockFlag into R
    • Context switch from thread A to B
  • ThreadB: load lockFlag into R
    • Compare value: it’s false
    • Store true to lockFlag
    • Enter critical section
  • Context switch from thread B back to A
  • Thread A:
    • compare R and value: it’s false, so proceed
    • Store true to lockFlag
    • Enter critical section
  • So both can be in critical section at same time.

Attempt

  • Use a “turn” variable:

int turn =0; is either 0 or 1…

public void WanttoEnterCS(int i) { while (turn!=i) Thread.currentThread().yield(); // go from running back to run queue give someone else a // chance. }

public void finishedInCS(int i) { turn = other(i); // this sets I to something other than i }

What’s the matter with this?

  • Hint -- what happens if there’s only one

thread that wants to run?

  • This is Starvation in the absence of

contention.

page 19

What’s wrong with this?

  • Deadlock.

page 20

Deadlock problem with

  • Both threads set their flags at more or less

the same time

  • Both threads enter the while loop and see

that the other’s flag is set. They then busy

wait forever.

page 21

Use of volatile setting for flag

  • Book says “it’s important in how this

attempt enforces mutual exclusion. Why?

page 22

Use of volatile setting for flag

  • Without the volatile setting, both threads

could set their flags, but neither would see

the other’s flag set, so both would enter the

CS.

page 23

Attempt

public void wantToEnterCS(int I) { desireCS[I].value = true; while (desireCS[other(I)] .value) { desireCS[i].value = false; // back off Thread.currentThread().yield(); desireCS[i].value = true; }

public void finishedInCS(int I) { desireCS[I].value = false; // unset flag } (^) page 24

What’s wrong with this?

  • Livelock -- both threads back off and try again in lockstep
  • Hartley claims that this livelock is unlikely (certain?) to last forever, due to differences in time slicing and clock speed. So this would be considered “almost correct” but with an efficiency defect in that the livelock will sometimes slow down entry into the CS.

page 25

Dekker’s attempt

public void wantToEnterCS(int I) { // preprotocol desireCS[I].value = true; while (desireCS[other(I)].value) { if (turn !=I) { // take turns backing off. desireCS[I].value = false; // back off while (turn !=I) // busy wait Thread.currentThread().yield(); desireCS[I].value=true; } // end if } // end while }

public void finishedInCS(int I) { // postprotocol desireCS[I].value = false; turn = other(I); } page 26

What kinds of problems, if any, does

Dekker’s algorithm have with

  • Enforcement of mutual exclusion
  • Deadlock/livelock
  • Starvation in the presence of contention
  • Starvation in the absence of contention

page 27

The semantics of yield()

  • public static void yield()
    • Causes the currently executing thread object to temporarily pause and allow other threads to execute.
  • Java Language Reference: “In some cases, a large number of threads could be waiting on the currently running thread to finish executing before they can start executing. To make the thread scheduler switch from the current running thread to allow others to execute, call the yield() method on the current thread. In order for yield() to work, there must be at least one thread with an equal or higher priority than the current thread..” page 28

Correctness

  • While it is sufficient to find a scenario to prove that something doesn’t work,
  • To prove that an algorithm is correct, you must show that it works under all possible scenarios.
  • Usually the way that this is done is to show that it is impossible for it to behave incorrectly.
  • This involves insight, using a combination of logic and programming language semantics.

Peterson’s attempt (mutual

exclusion for two threads)

public void wantToEnterCS(int I) { // preprotocol desireCS[I].value = true; last = other(I); while (desireCS[other(I)].value && last == other(I)) // busy wait Thread.currentThread().yield(); }

public void finishedInCS(int I) { // postprotocol desireCS[I].value=false; }

Proof of correctness

  • Prove that the algorithm
    • enforces mutual exclusion correctly by showing that it is impossible for two threads to be in the critical section simultaneously.
    • doesn’t allow deadlock (i.e. it’s impossible).
    • doesn’t allow starvation in the presence of contention
    • doesn’t allow starvation in the absence of contention
  • A separate proof for each point, each done as a proof by contradiction.