Week 9 Flashcards

1
Q

What are the design goals for counters.

A

Correctness: how do we make the above operations works as expected with multiple threads?

Performance: how do we allow many threads to access the structure/

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

What is a sloppy counter?

A

> Performance goal is perfect scaling: threads should complete just as quickly on multiple processors as a single thread does on one.

> How do we improve scaling for counters? We give each core its own local counter
- Be we also relax some assumptions: We no longer assume the counter is exact, only that it is close to the real value.

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

What are condition variables?

A

A condition variable is a synchronization mechanism allowing threads to:
- wait until a condition is true, and
- signal other threads when the condition becomes true.

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

What is the bounded buffer problem?

What’s the condition in the bounded buffer problem.

A

Imagine we have one or more producer threads placing items in a buffer.

We also have one or more consumer threads grabbing items from that
buffer.

Problem: The buffer is a shared resource; how do we synchronize access?

What’s the condition in the bounded buffer problem?
- wait: notify me when the buffer has data
- signal: I just put data in the buffer, so notify the threads waiting for the
buffer to have data

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

What are semaphores?

A

Semaphores are another synchronization primitive. Can initialize the semaphore to any integer, but can only increment or decrement after that.

> They can be used as both a lock and a condition variable.
Unlike locks and condition variables, semaphores can be used to synchronize across different processes.
POSIX Inferface: sem_wait() and sem_post()
- sem_wait(): decrement the value of the semaphore s by one and wait/sleep if is if negative
- sem_post(): increment the value of the semaphore s by one, if there are one or more threads waiting, wake one.

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

How can we use a semaphore as a lock?

A
  • Surround the critical section with a sem_wait and sem_post.
  • Set the initial value of the semaphore to 1.
  • How does the semaphore’s value change if we have more than two threads? A negative value is equal to the number of threads
    waiting.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or false, When you create the semaphore, you can initialize its value to any integer, but after that the only operations you are allowed to perform are increment (increase by one) and decrement (decrease by one). You cannot read the current value of the semaphore

A

True

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

True or false, when you signal a semaphore, you know whether another thread is waiting.

A

False. When you signal a semaphore, you don’t necessarily know whether another thread is waiting, so the number of unblocked threads may be zero or one.

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

What are deadlocks?

A

Deadlocks occur when a thread or set of threads are waiting for each other to finish and thus nobody ever does.

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

A deadlock only occurs if all of the following conditions are met:

A
  • mutual exclusion: threads claim exclusive access to shared resources m(e.g., thread grabs a lock)
  • hold-and-wait: a thread can hold some resources while requesting others,
  • no resource preemption: the system cannot forcibly take a resource from
    a thread holding it,
  • circular wait: meaning that Thread A is waiting for Thread B which is waiting for Thread C which is waiting for Thread A.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly