Week 8 Flashcards

1
Q

What is concurrency?

A

The ability of our machine to handle and execute multiple different tasks at seemingly same time.

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

At the process level, programmers can manage concurrency using __________

A

threading

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

Unless explicitly synchronized, processes/threads may:
Be run in _________
Be stopped and restarted at ________
Remain stopped for _____________

A

any order

any time

arbitrary lengths of time

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

Why is the operating system one of the most difficult concurrent programs to write?

A

It is multiplexing access to hardware resources and therefore sharing a great deal of state between multiple processes.

It frequently uses many threads to hide hardware delays while servicing devices and application requests.

Lots of shared states plus alot of threads.

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

What is a race condition?

A

A race condition is when the output of a process is unexpectedly dependent on timing or other events.

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

Processes contain ________ , multiple ____ can be executing concurrently within a single process.

A

threads

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

A process thread can be thought of as …

A

an abstraction of the CPU state

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

Both processes and threads are used for concurrency. What do they do?

A

Processes allow for concurrency to be managed at the OS level

Threads allow for concurrency to be managed at the process level.

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

Why use threaeds instead of separate processes?

A

When the program needs cheap communication.

Lowers the cost of a context switch

Gives the programmer additional control over concurrent execution.

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

Communication between threads tends to use ________ objects in memory

A

Communication between threads tends to use shared objects in memory.

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

Thread stacks and local variables are intended to be _________

A

Thread stacks and local variables
are intended to be private

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

What is the difference between concurrency and atomicity?

A

Concurrency: the illusion that multiple things are happening at once. (requires stopping or starting any thread at any time.

Atomicity: the illusion that a set of separate actions occurred all at once.

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

What is a critical section?

A

> A critical section contains a series of instructions that only one thread should be executing at any given time.

> This series of instructions should look atomic with respect to other
threads.

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

What are these critical section requirements?

Mutual Exclusion

Progress/Fairness

Performance

A

> Mutual Exclusion: this is the most basic property. Only one thread
should be executing in the critical section at one time.

> Progress/Fairness: all threads should eventually be able to proceed through the critical section.

> Performance: we want to keep critical sections as small as
possible without sacrificing correctness.

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

How do we enforce critical sections?

There are 2 possible approaches.

A

> Don’t stop: On uniprocessors a single thread can prevent other threads from executing in a critical section by simply not being descheduled.

> Don’t enter: More generally we need a way to force other threads—potentially running on other cores—not to enter the
critical section while one thread is inside. How do we do this? Locks!

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

What is a lock?

A

A lock is just another variable in your program.

A lock is either free/unlocked/available or held/acquired/locked by exactly one thread.

If a thread holds a lock then they are (presumably) in the critical section

17
Q

What are the 3 things that we care about when evaluating locks?

A
  • Correctness (i.e., Mutual Exclusion): this is the most basic property. Only one thread should be executing in the critical
    section at one time.
  • Progress (i.e., fairness): all threads should eventually be able to proceed through the critical section.
    • Performance: what is the overhead of the lock? How is it affected by multiple processors?
18
Q

What is test and set?

A

Test-and-set: write a memory location and return its old value.

19
Q

What is a spinlock?

A

Lock for the fact that it guards the critical section.

spin describing the process of acquiring it.

Spinlocks are rarely used on their own to solve synchronization problems, but are commonly used to build more useful
synchronization primitives.

20
Q

Are spin lock…
correct?
fair?
performant?

A

Are spin locks correct? Yes, they provide mutual exclusion.

Are spin locks fair? No, they don’t employ any mechanism to ensure other threads get to execute.

Are spin locks performant? Sometimes…

21
Q

SPIN LOCKS

When should we go to sleep?

When should we spin?

A

Sleep if the critical section is long.

Spin if the critical section is short.

22
Q

What is a busy waiting.

A

Busy waiting: threads wait for the critical section by pounding on the door, executing the test-and-set repeatedly.

Bad on a multicore system. Worse on a single core system! Busy waiting prevents the thread in the critical section from making progress!

23
Q

What is compare and swap.

A

It will test whether the stored value is equal to some test value. If so, update the the
stored value to some new value.

Compare-and-swap is more powerful than test-and-set, but in the simple case of spin locks the code and behavior is essentially the same.

These are often used for lock-free synchronization.

24
Q

What is load-linked and store-conditional.

A

First, load a value from memory
using the load-link instruction.

> Second, store a value into that address using store-conditional, but only if the value at that address hasn’t been modified in the meantime.

  • Again, we can use these instructions to implement a spin lock
25
Q

What is fetch and add?

A

Automatically increments a value while returning the old value.

We can use fetch-and-add to build a lock
with fairness: a ticket lock

26
Q

True or false, we can solve the performance problems of spin locks with just hardware.

A

False. We can’t solve the performance problems of spin locks just with hardware,
we have to rely on the OS.

27
Q

What does yield() do?

A

The system call yield() allows a thread to deschedule itself, moving the
thread from the running state to the ready state.

28
Q

What does fetch and add do?

A

fetch-and-add: automatically increments a
value while returning the old value.

  • We can use fetch-and-add to build a lock with fairness: a ticket lock.