5. Deadlock and Exec Flashcards

1
Q

What do locks do?

A

Locks protect access to a shared resource.

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

Walk through the steps of acquiring deadlock with two threads and two shared resources.

A

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.

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

What is deadlock?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can a single thread deadlock?

A

Thread A acquires the lock for Resource 1 and then tries to reacquire Resource 1.

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

How do you implement a recursive lock?

A

You allow a thread to reacquire a lock that it already holds, as long as calls to acquire are matched by calls to release.

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

Why would you want to implement a recursive lock?

A

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.

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

What four conditions must be in place to result in a deadlock?

A
  1. Protected access to shared resources (which implies waiting).
  2. No resource preemption, meaning that the system cannot forcibly take a resource from a thread holding it.
  3. Multiple independent requests, meaning a thread can hold some resource while requesting others.
  4. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe the “Dining Philosophers” problem.

A

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.

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

What is starvation?

A

The condition in which one or more threads do not make progress.

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

How is starvation different than deadlock?

A

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

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

Which is better, deadlock or a race condition?

A

Deadlock, because the problem is much easier to detect. Progress stops as soon as the issue arrises, pointing directly to the culprits.

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

What are the four stages of the process lifecycle?

A
  1. Birth w/ fork(). 2. Change w/ exec(). 3. Death w/ exit(). 4. Afterlife w/ wait()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does exec() do?

A

The exec() family of system calls replaces the calling process with a new process loaded from a file

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

What does ELF stand for?

A

Executable and Linkable Format

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

What must the executable file that exec() remakes a process with contain?

A

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.

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

How does argument passing work when calling exec()?

A

The process calling exec() passes arguments to the process that will replace it through the kernel.

The kernel retrieves the arguments from the process after the call to exec() is made.

It then pushes them in to the memory of the process where the replacement process can find them when it starts executing.

(This is where main gets argc and argv)

Note: exec() does not return (on success)

17
Q

Does exec() mess with file handles when modifying a process?

A

No. Exec() does not modify the file table of the calling process. Fork() did work to duplicate the file table and possibly set up pipes between the parent and child processes, so it would be counterproductive to upend any work fork() accomplished through manipulation of the file table.