q5 Flashcards

(34 cards)

1
Q

What is the basic idea of locks?

A

Ensure that any critical section executes as if it were a single atomic instruction.

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

What is a lock variable’s state when it is available?

A

No thread holds the lock.

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

What happens when a thread calls lock() on a lock that is already held?

A

It will not return while the lock is held by another thread.

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

What is the purpose of the unlock() routine?

A

To change the state of the lock to available (free) again.

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

What is pthread_mutex_t?

A

The name that the POSIX library uses for a lock.

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

What is the benefit of using different locks for different variables?

A

Increases concurrency by allowing more threads to be in locked code at once.

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

What are the basic criteria for evaluating locks?

A
  • Mutual exclusion
  • Fairness
  • Performance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the problem with controlling interrupts for critical sections?

A

Turning off interrupts for extended periods can lead to lost interrupts and serious system problems.

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

What is the purpose of the test-and-set instruction?

A

To support the creation of simple locks by providing atomic operations.

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

What does the Compare-And-Swap instruction do?

A

Tests whether the value at the address is equal to expected and updates it if so.

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

True or False: Spin locks provide fairness guarantees.

A

False.

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

What is a simple approach to avoid spinning in locks?

A

Yield the CPU to another thread when going to spin.

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

What function does park() serve?

A

Puts a calling thread to sleep.

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

What is the structure of a lock_t in the context of using queues?

A
  • int flag;
  • int guard;
  • queue_t *q;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the role of the guard in the lock_t structure?

A

To protect the queue.

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

What happens when a thread calls lock() on a lock_t that is already held?

A

The thread spins until it can acquire the lock.

17
Q

Fill in the blank: The lock is acquired when the flag in lock_t is set to _______.

18
Q

What is the consequence of too much spinning in hardware-based spin locks?

A

Wastes an entire time slice doing nothing but checking a value.

19
Q

Why is hardware support needed for locks?

A

To provide atomic instructions necessary for mutual exclusion.

20
Q

What is the purpose of the function lock(lock_t *m)?

A

To acquire a lock using a spinlock mechanism and queue management

The function spins to acquire the lock and uses a queue to manage waiting threads.

21
Q

What does the TestAndSet function do in the lock function?

A

It attempts to set the guard lock and returns the previous value

This is used to check if the lock is already held by another thread.

22
Q

What happens if the lock is successfully acquired in the lock function?

A

m->flag is set to 1 and m->guard is set to 0

This indicates that the lock is now held by the current thread.

23
Q

What occurs when the lock is not available in the lock function?

A

The current thread is added to the queue and calls park()

This puts the thread to sleep until it can acquire the lock.

24
Q

What is the purpose of the unlock(lock_t *m) function?

A

To release the lock held by the current thread

It also manages the queue of waiting threads.

25
What does the function queue_empty(m->q) check in the unlock function?
It checks if there are any threads waiting for the lock ## Footnote If the queue is empty, it indicates no threads are waiting.
26
What is the significance of the setpark() system call in the locking mechanism?
It indicates that a thread is about to park, allowing for quicker wakeup if interrupted ## Footnote This prevents threads from sleeping indefinitely.
27
What are futexes and where are they used?
Futexes are fast user-space mutexes used by Linux ## Footnote They allow for efficient lock management and sleeping threads.
28
What does the futex_wait(address, expected) function do?
It puts the calling thread to sleep if the value at address matches expected ## Footnote If it doesn't match, the call returns immediately.
29
What happens in the mutex_lock(int *mutex) function when the lock is acquired?
The high bit of the mutex is cleared, indicating it is locked ## Footnote This is done using atomic operations.
30
What does atomic_add_zero(mutex, 0x80000000) check in the mutex_unlock function?
It checks if there are any other threads waiting for the mutex ## Footnote This operation targets the high bit which indicates lock status.
31
What is the concept of a two-phase lock?
A locking mechanism that spins briefly before sleeping if the lock cannot be acquired ## Footnote It combines spinning and sleeping to optimize performance.
32
What are the two phases of a two-phase lock?
* First phase: The lock spins hoping to acquire it * Second phase: The caller sleeps if the lock is not acquired ## Footnote This allows for a balance between busy waiting and efficient sleeping.
33
What hardware support is mentioned for implementing locks?
* Test-and-set (RISC-V) * Compare-and-swap (SPARC, x86) ## Footnote These are atomic operations that facilitate lock mechanisms.
34
What OS support is provided for locking mechanisms?
* park() * unpark() * setpark() * futex ## Footnote These functions help manage thread sleeping and waking efficiently.