Implementing Locks - Operating Systems - Notes | CMPSCI 377, Exams of Operating Systems

Material Type: Exam; Professor: Berger; Class: Operating Systems; Subject: Computer Science; University: University of Massachusetts - Amherst; Term: Fall 2005;

Typology: Exams

Pre 2010

Uploaded on 08/19/2009

koofers-user-o7c-1
koofers-user-o7c-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMPSCI 377 Operating Systems Fall 2005
Lecture 7: October 11
Lecturer: Emery Berger Scribe: Rob Tate, Lou Karadashkov
Today:
Implementing Locks
Semaphores
Bonus topics!
7.1 Implementing Locks
7.1.1 Atomic Instructions
Atomic instructions are provided by the architecture.
-Cannot be interrupted
-Needed to build locks
-Test and Set (most architectures)
-Exchange(x86)
-LLSC–Load Link Store Condition(PowerPC)
Example(this must be atomically provided by architecture):
int testset (int value)
{
int old = value;
value = 1;
return old;
}
7.1.2 Using “Test and Set”
To acquire, spin in a while loop, like “while(testset(value)){
To release, set value back to 0
-Busy-Loops are bad news.
-“Priority Inversion”
-High priority threads might get the lock, but they should always get it before lower priorities
-Will actually work
Locks should be optimized for the common case, that they are uncontended.
7-1
pf3

Partial preview of the text

Download Implementing Locks - Operating Systems - Notes | CMPSCI 377 and more Exams Operating Systems in PDF only on Docsity!

CMPSCI 377 Operating Systems Fall 2005

Lecture 7: October 11

Lecturer: Emery Berger Scribe: Rob Tate, Lou Karadashkov

Today:

  • Implementing Locks
  • Semaphores
  • Bonus topics!

7.1 Implementing Locks

7.1.1 Atomic Instructions

Atomic instructions are provided by the architecture. -Cannot be interrupted -Needed to build locks -Test and Set (most architectures) -Exchange(x86) -LLSC–Load Link Store Condition(PowerPC)

Example(this must be atomically provided by architecture):

int testset (int value) { int old = value; value = 1; return old; }

7.1.2 Using “Test and Set”

To acquire, spin in a while loop, like “while(testset(value)){” To release, set value back to 0 -Busy-Loops are bad news. -“Priority Inversion” -High priority threads might get the lock, but they should always get it before lower priorities -Will actually work Locks should be optimized for the common case, that they are uncontended.

7-2 Lecture 7: October 11

7.1.3 A Better Solution

Let’s use a queue:

void acquire(){ while(true){ if (testset(value)){ put thread on queue go to sleep }else{ break; } } }

void release(){ value = 0; wake up threads }

How many threads to wake up? -If we wake up only one, we might get Priority Inversion -If we wake them all up, we might get “Thundering Herd” -We should use a priority queue and wake up the one at the front

Provides Mutual Exclusion, but... -Deadlock can easily happen. -Someone can forget to unlock. -Someone can try to lock twice.

What if we want to let two people access but not three? Can’t do this with locks. We need Semaphores!

7.2 Semaphores

7.2.1 What are Semaphores?

Semaphores are basically non-negative integer counters with atomic methods for incrementing and decre- menting. These methods are used to make wait and signal methods.

-wait() decreases semaphore by one unless it is zero, in which case it goes on queue -also called P() -signal() increases semaphore by one and wakes up thread(s) -also called V()

  • Think of semaphores as traffic lights that know how many cars should be allowed through.