q5 Flashcards
(34 cards)
What is the basic idea of locks?
Ensure that any critical section executes as if it were a single atomic instruction.
What is a lock variable’s state when it is available?
No thread holds the lock.
What happens when a thread calls lock() on a lock that is already held?
It will not return while the lock is held by another thread.
What is the purpose of the unlock() routine?
To change the state of the lock to available (free) again.
What is pthread_mutex_t?
The name that the POSIX library uses for a lock.
What is the benefit of using different locks for different variables?
Increases concurrency by allowing more threads to be in locked code at once.
What are the basic criteria for evaluating locks?
- Mutual exclusion
- Fairness
- Performance
What is the problem with controlling interrupts for critical sections?
Turning off interrupts for extended periods can lead to lost interrupts and serious system problems.
What is the purpose of the test-and-set instruction?
To support the creation of simple locks by providing atomic operations.
What does the Compare-And-Swap instruction do?
Tests whether the value at the address is equal to expected and updates it if so.
True or False: Spin locks provide fairness guarantees.
False.
What is a simple approach to avoid spinning in locks?
Yield the CPU to another thread when going to spin.
What function does park() serve?
Puts a calling thread to sleep.
What is the structure of a lock_t in the context of using queues?
- int flag;
- int guard;
- queue_t *q;
What is the role of the guard in the lock_t structure?
To protect the queue.
What happens when a thread calls lock() on a lock_t that is already held?
The thread spins until it can acquire the lock.
Fill in the blank: The lock is acquired when the flag in lock_t is set to _______.
1
What is the consequence of too much spinning in hardware-based spin locks?
Wastes an entire time slice doing nothing but checking a value.
Why is hardware support needed for locks?
To provide atomic instructions necessary for mutual exclusion.
What is the purpose of the function lock(lock_t *m)?
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.
What does the TestAndSet function do in the lock function?
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.
What happens if the lock is successfully acquired in the lock function?
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.
What occurs when the lock is not available in the lock function?
The current thread is added to the queue and calls park()
This puts the thread to sleep until it can acquire the lock.
What is the purpose of the unlock(lock_t *m) function?
To release the lock held by the current thread
It also manages the queue of waiting threads.