chapter 28: locks Flashcards

1
Q

what are the 2 states of a lock?

A

available or acquired

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

where are locks usually?

A

at the beginning of a critical section so only one thread can enter at a time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what do pthread locks use?

A

mutex - mutual exclusion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is a coarse-grained locking strategy?

A

just one big lock surround CS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is a fine-grained locking strategy?

A

different locks to protect different data structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what help do we need to build a lock?

A

hardware help

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is the criteria for a lock?

A

mutual exclusion
fairness - avoid starvation. fair shot at acquiring lock
performance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

1st solution: controlling interrupts

A

lock : disable interrupt
unlock: enable interrupt

positives: it’s simple

negatives:
- allows any thread to perform privileged operations (greedy program calls lock and monopolize the CPU)
OS never regains control and has to restart
- does not work on multiprocessors
- turning off interrupts can cause lost of interrupts which can lead to problems
- inefficient

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is Test and Set (Atomic exchange) (TAS)?

A

this is hardware support. test and set instruction aka atomic exchange.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

first attempt: a simple flag

A

using a simple flag to implement lock and unlock
flag is 1, lock is held
flag is 0, lock isnt held
if flag is 1 then spin-wait until it hits 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is the problem with simple flag approach?

A

one is correctness, with timely interrupts, we can easily produce both a case where both threads set the flag to 1 and both threads are able to enter CS. failed mutual exclusion.
two is performance, spin waiting wastes time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

using Test and Set to create working spin lock

A

just like simple flag but it works because it is atomic thanks to TAS. spin lock is the simplest type of lock to build, to work correctly, it requires a preemptive scheduler(a scheduler that interrupts via a timer)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

evaluating spin locks, how good are they?

A

correctness: good. it implements mutual exclusion
fairness: may lead to starvation
performance: single CPU - horrible
multiple CPUs - works reasonably well

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is compare and swap (CAS)?

A

another hardware support for atomic instruction
CAS is more powerful than TAS. however, making simple spin lock with it will be the same as TAS.
if actual ptr == expected
ptr = new
return actual

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is Load-linked and Store-Conditional

A

a pair of instructions to help build critical sections.
Load linked just loads instruction.
key difference is Store-Conditional which only succeeds if no intervening store to the address has taken place.
On success, returns 1 and updates ptr to value
On Failure, returns 0 and not updated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how does load-linked and store-conditional work as a spin lock?

A

if T1 calls lock() and executes the load-linked, returning 0. Before it attempts store-conditional, it is interrupted and T2 executes load-linked and returns 0. the key is that only 1 of these threads will succeed in updating the flag to 1 and the other will fail to acquire the lock.

17
Q

what is fetch-and-add?

A

hardware primitive that automatically increments a value while return the old value at a particular address.
can implement Ticket Locks which avoids starvation because each thread has a turn given by fetch and add. it is like queuing up with tickets.

18
Q

what is the main problem with spin locks?

A

performance: it’s wasting too much time on a single processor. spins too much. time wasted

19
Q

hardware helped with mutual exclusion, what can help with the performance, avoid spinning so much?

A

OS support!

20
Q

A simple approach: just yield

A

when need to spin: give up the cpu to another thread. thread can call yield() whenever it wants to give up the cpu. a thread can be ready, running, blocked. yield simply moves thread from running to ready. (descheduled)
2 threads it works quite well here
many threads will cause many context switches which is costly and wasteful still. Also, it still starves.

21
Q

what is the problem with the 2 previous approaches, spin and yield?

A

they leave too much to chance. The scheduler determines which thread runs next. potential for waste and no prevention from starvation

22
Q

Using Queues: sleeping instead of spinning

A

OS support to put thread to sleep and wake a thread up.
puts a thread to sleep if it tries to acquire a held lock.
wakes it when the locks is free.
we also need a queue to help control who gets the lock next to avoid starvation.

23
Q

problems with this Using Queues: sleeping instead of spinning approach?

A
  1. doesnt avoid spin waiting entirely but spin waiting is limited
  2. potential race condition, wakeup/waiting race. sleeps forever. this problem is solved by setpart() about to park before it parks
24
Q

what is a Two-Phase lock? (hybrid approach)

A

first phase: it spins for a bit first in case the lock is about to be released.
2nd phase: it sleeps