Module 4 - Threads, synchronisation and deadlock Flashcards

1
Q

How do threads differ from processes?

A

The threads share heap, data segment, text
segment and the file descriptor table. Different processes do not share these resources.

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

Why is it more “expensive” to create a new process compared to creating a new thread?

A

Allocating memory and resources for process creation is costly. Because threads share the resources of the process to which they belong, it is more economical to create and context-switch threads.

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

In short, explain the many-to-one user level thread model. Also explain what happens if one of the threads makes a blocking system call in the many-to-one user level thread model.

A

Many user-level threads mapped to
a single kernel thread.

Only one thread can access the kernel at a
time, hence threads cannot run in parallel on multiprocessors.

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

What is meant by an atomic operation? Give examples of non-atomic operations.

A

In concurrent programming, an operation (or set of operations) is atomic if it appears to the rest of the system to occur at once without being interrupted.

lw (load), addi (increment), sw (store) are non-atomic operations.

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

Define the following terms: Race condition, Data race, Critical section and Mutual exclusion.

A

A race condition or race hazard is the
behaviour of an electronic, software or
other system where the output is dependent on the sequence or timing of
other uncontrollable events.

A data race occurs when two instructions from different threads access the same memory location and:
‣ at least one of these accesses is a write
‣ and there is no synchronization that is
mandating any particular order among these accesses.

A critical section is defined as part of a
program that should not be concurrently
executed by more than one of the program’s concurrent processes or threads at a time.

The requirement of ensuring that no two concurrent processes or threads are in the same critical section at the same time.

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

What is meant by a spin lock? What is meant by busy waiting?

A

A spinlock is a lock where a task simply waits in a loop (“spins”) repeatedly checking until the lock becomes available.

While a task is in its critical section, any other task that tries to enter its critical section must loop continuously in the entry code.

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

In the context of mutual exclusion, what is meant by starvation?

A

A mutex can encounter starvation issues when it consistently tries to acquire a lock that it is never able to get.

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

What are the limitations of Petersson’s solution to the mutual exclusion problem?

A

Software-based solutions such as Peterson’s are not guaranteed to work on modern computer architectures (without using memory barriers).

Peterson’s solution only works for two concurrent tasks.

While Peterson’s original formulation worked with only two processes, the algorithm can be generalized for more than two.

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

Name two atomic CPU instructions that can be used to implement synchronization locks.

A

The TestAndSet instruction atomically first sets the value at the target address to True and return the old value stored at the target address.

The Swap instruction atomically
swaps the content of two memory
locations a and b.

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

How can spin locks be constructed using the two atomic instructions from above?

A

Check pngs in downloads…

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

What operations can be performed on a semaphore and how do these operations work?

A

wait() - if counter > 0, decrement counter,
otherwise wait until counter becomes > 0, then decrement counter.

signal() - Increments the semaphore counter.

init() - create/allocate semaphore.

destroy() - delete/deallocate semaphore.

block() - block caller and add the caller to the semaphore wait list.

wakeup() - remove one task from the semaphore wait list and resume execution of this task.

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

What operations can be performed on a mutex lock and how do these operations work?

A

init() - create/allocate mutex lock.
acquire() - busy wait for lock to become false, then grab the lock (set mutex to true).
release() - set lock to false.
destroy() - Maybe same as semaphore (probably).

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

What is the difference between a mutex lock and a semaphore?

A

The correct use of a semaphore is to do signaling from one task to another.

A mutex is meant to be taken and released, always in that order, by each task that uses the shared resource it protects.

By contrast, tasks that use semaphores either signal or wait — not both. For example a consumer waits for a producer to produce data, and a producer signals the consumer when data is available.

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

When implementing semaphores and mutex locks, how can busy waiting be avoided?

A

By implementing a waiting queue.

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

Name and explain the necessary conditions for deadlock?

A

Deadlock will arise iff (if and only if) these four conditions holds simultaneously.

Mutual exclusion: only one task at a time can use a resource instance.

Hold and wait: a task holding at least one resource is waiting to acquire additional resources held by other tasks.

No preemption: a resource can be released only voluntarily by the task holding it, after that task has completed its task.

Circular wait: there exists a set {T0, T1, …, Tn-1} of waiting tasks such that T0 is waiting for a resource that is held by T1, T1 is waiting for a resource that is held by T2, …, …, …., Tn-2 is waiting for a resource that is held by Tn-1, and Tn-1 is waiting for a resource that is held by T0.

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

Explain the differences between deadlock prevention and deadlock avoidance.

A

Deadlock prevention is implemented by constraining how requests for resources can be made in the system and how they are handled (system design). The goal is to ensure that at least one of the four necessary conditions for deadlock can never hold.

Deadlock avoidance is implemented by making the system dynamically consider every request and decide whether it is safe to grant it at this point. Even if a resource is free to use, it may not be safe to grant a
request in order to stay out of deadlock in the future. Requires additional a priori information regarding the overall potential use of each resource for each task (worst case future use of resources). Allows for more concurrency compared to deadlock prevention.

17
Q

Explain how deadlock prevention can be used to prevent circular wait.

A

Make circular wait impossible by impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.

18
Q

What conclusions regarding deadlock can be made using a resource allocation graph (RAG)?

A

If a RAG contains no cycles 㱺 no deadlock.

If a RAG contains a cycle:

-> If only one instance per resource type 㱺 deadlock.

-> If several instances per resource type 㱺 possibility of deadlock.

19
Q

Explain the Dining philosophers problem.

A

They have difficulty taking turns when eating rice.

20
Q

Do q 20-26 when studying for the exam.

A