5. Deadlock and Exec Flashcards
What do locks do?
Locks protect access to a shared resource.
Walk through the steps of acquiring deadlock with two threads and two shared resources.
Thread A runs and grabs the lock for Resource 1. Thread B runs and grabs the lock for Resource 2. Context switch to thread A which tries to grab lock for Resource 2 and goes to sleep. Context switch to thread B which tries to grab the lock for Resource 1 and goes to sleep.
Both threads are asleep with no thread capable of waking either up. Deadlock.
What is deadlock?
Deadlock occurs when a thread or set of threads are waiting for each other to finish and thus nobody ever does.
(Circular dependency; cycle of dependencies)
How can a single thread deadlock?
Thread A acquires the lock for Resource 1 and then tries to reacquire Resource 1.
How do you implement a recursive lock?
You allow a thread to reacquire a lock that it already holds, as long as calls to acquire are matched by calls to release.
Why would you want to implement a recursive lock?
In case a single thread needs to acquire a lock through a function call foo() and then acquire it again within foo() through another call.
What four conditions must be in place to result in a deadlock?
- Protected access to shared resources (which implies waiting).
- No resource preemption, meaning that the system cannot forcibly take a resource from a thread holding it.
- Multiple independent requests, meaning a thread can hold some resource while requesting others.
- Circular dependency graph, meaning that Thread A is waiting for Thread B which is waiting for Thread C which is waiting for Thread D which is waiting for Thread A
Describe the “Dining Philosophers” problem.
5 philosophers come to a dinner party, each carrying one chop stick. Two chop sticks are required to eat, so no philosopher is able to make progress on their meal with their own single chop stick. They must come up with a synchronization plan so that they can all eat using the shared resources available to them.
What is starvation?
The condition in which one or more threads do not make progress.
How is starvation different than deadlock?
Starvation differs from deadlock in that some threads make progress and it is, in fact, those threads that are preventing the “starving” threads from proceeding
Which is better, deadlock or a race condition?
Deadlock, because the problem is much easier to detect. Progress stops as soon as the issue arrises, pointing directly to the culprits.
What are the four stages of the process lifecycle?
- Birth w/ fork(). 2. Change w/ exec(). 3. Death w/ exit(). 4. Afterlife w/ wait()
What does exec() do?
The exec() family of system calls replaces the calling process with a new process loaded from a file
What does ELF stand for?
Executable and Linkable Format
What must the executable file that exec() remakes a process with contain?
A complete blueprint indicating how the address space should look when exec() completes.
It should lay out what the contents of memory should be and where the first thread starts executing.