Condition Variables, Semaphores & Deadlocks Flashcards

1
Q

Condition variable signals

A
  • wait: assumes the lock is held when wait() is called, puts caller to sleep + releases the lock (atomically), when awoken, reacquires lock before returning
  • signal: wake a single waiting thread
  • broadcast: wake all waiting threads
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Rules of thumb for condition variables

A
  • keep state in addition to CV’s
  • always do wait/signal with lock held
  • whenever thread wakes from waiting, recheck state (use “while”)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

enumerate types of synchronization objectives

A
  • spinlocks
  • (counting) semaphore
  • Mutex (lock)
  • condition variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

summarize spinlocks’ respective
operations’ semantics

A

lock/unlock
uses busy waiting and atomic instructions (such as test-and-set) to ensure mutual exclusion. Wates CPU time, thus only recommended for short critical sections. Preemption is problematic, so pure spinlocks are most common in kernel code with interrupts disabled. Doesn’t make sense on single-processor systems

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

summarize semaphores’ respective
operations’ semantics

A

wait(sem)/signal(sem)
Each call to wait decrements the counter of the semaphore. If the counter falls below 0, the thread/process executing wait os blocked and appended to the semaphore’s queue. A call to signal increments the counter of the semaphore. If it is still less or equal to zero, a thread/process is removed from the queue and unblocked. The counter isn’t directly accessible for users of a semaphore.

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

summarize mutexes’ respective
operations’ semantics

A

lock(m), unlock(m)
A counting semaphore whose counter can only take values 0 or 1. Calls to signal to wake up a thread from sem’s queue

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

summarize condition variables’ respective
operations’ semantics

A

wait(cond, mutex), signal(cond)
Always used with a mutex. Allows a thread to acquire a lock, check for a certain condition, and go to sleep if the condition isn’t met. The lock is automatically released when the thread is blocked and reacquired when the thread is unblocked.

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

Condition variables vs semaphores

A

-CV have no state
-Semaphores have state: track integer value

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

which synchronization primitives can be built from others

A

locks from semaphores
CV from semaphores
Semaphores from locks + CV

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

Deadlock conditions

A
  1. Mutual exclusion: limited access to resource/ resources can’t be shared between processes
  2. Hold and Wait: Wait for next resource while already holding at least one
  3. No preemption: Once the resource is granted, it can’t be taken away but only handed back voluntarily
  4. Circular wait: Possibility of circularity in graph of requests
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Deadlock countermeasures

A
  1. Prevention: pro-active, make deadlocks impossible to occur
  2. Avoidance: decide on allowed action based on knowledge
  3. Detection: react after deadlock happened (recovery)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the definition of a deadlock?

A

It is a situation in which several activities can’t make any progress, because each activity is waiting for a resource that is assigned to another activity, and no activity releases any of its resources.

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

For each condition necessary for deadlocks, give an example of how they can be prevented by breaking the condition.

A

Mutual Exclusion: Spooling; to avoid multiple processes from directly accessing a non-sharable resource
Hold And wait: allocate all resources atomically; once a process holds any resources, it must not allocate additional resources
No preemption: allowing resources to be revoked
Circular wait: ordering resources numerically and only allow multiple resources to be allocated in a specified order

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

Why is disabling interrupts a privileged instruction?

A

Preemptive scheduling is realized by using the timer interrupt to involuntarily enter the kernel while a user space process is executing.
If a process in user space could disable interrupts, it could gain infinite CPU time, thereby taking over the system.

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