Exam 2 (Concurrency and Deadlocks) Flashcards

1
Q

Why do we need synchronization?

A

Activities share resources, and it’s important to coordinate their progress.

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

What type of coordination is this: John and Mary are printing 10 page documents on the same printer. We don’t want their pages to interleave!

A

Exclusion

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

What type of coordination is this: John and Mary are sharing a bank account. John deposits $10. Mary should be allowed to withdraw only after this deposit.

A

Ordering

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

Types of shared resources

A

Physical (terminal, disk, network, …) or Logical (files, sockets, memory, …)

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

What is mutual exclusion?

A

Think of race conditions… If two threads are accessing the same array, we need to block one from executing while the other is executing to avoid a race condition.

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

What is a critical section?

A

The set of instructions where race conditioning could occur, in the event of an array it would be Load, Add, Store.

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

Implementing Mutex: Disable Interrupts

A

Stops scheduling other threads. It’s easy to implement, but we don’t want to give that much power to users, it doesn’t work on a multiprocessor, and disables multiprogramming even when there is no critical section access.

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

Implementing Mutex: Problem with Software Solutions

A

We can have a variable to indicate a thread accessing the critical section, but this has a race condition too so it doesn’t really work!

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

Implementing Mutex: What we want

A

Mutual exclusion (correctness), Granting access to a critical section if not being used (progress), no process should have to wait forever (bounded waiting).

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

Implementing Mutex: Strict Alternation

A

Uses a variable to indicate turns. Seems to work, but requires threads to alternate, and doesn’t meet the progress requirement. We can fix this by having an array of “flags”, but both can set their flags to true and wait indefinitely.

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

Implementing Mutex: Peterson’s Algorithm

A

Each algorithm has two variables, for flag and turn. If flag is true, a process wants to enter Critical Section. This is allowed to happen when the other process doesn’t want to enter, or if the process has priority.

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

Implementing Mutex: Multiple Thread Solution

A

Use the Bakery Algorithm. Every thread has a unique id, like tickets in a bakery. They are serviced in increasing tickets, and need to use some kind of tie breaker.

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

Implementing Mutex: Specialized Instructions

A

Functions are still atomic, we can use Bool Test&Set or Swap. Parameters are passed by reference.

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

Test&Set

A

Have a boolean indicating TRUE before entering the critical section, and then set it to FALSE when finished. Machine instruction will end execution.

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

Swap

A

Swaps a key and the lock. When exiting the CS, lock becomes false.

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

Spinning vs Blocking

A

Blocking can make it so that we can make sure we don’t unnecessarily occupy CPU cycles. It’s the job of the process changing the condition to move the process from blocked to ready.

17
Q

Example of Blocking Implementations

A

Mutex_Lock and Mutex_Unlock from pthread library - remember P1

18
Q

C_wait and C_signal

A

Used to implement synchronization on a multithreaded processor. A thread blocked on C_wait will return when another performs a C_signal. C_wait will have to be inside a mutex_lock.

19
Q

Semaphores

A

A data type which we can use P and V on, P is analogous to waiting, V is analogous to signaling. A Semaphore will have a count that is incremented on a V call and decremented on a P call. We will block the Semaphore if the count is negative.

20
Q

Bounded Buffer problem

A

A queue of finite size implemented as an array, which could cause race conditions when accessed by multiple threads. Also, need to wait when appending to buffer when it is full or removing when empty.

21
Q

Readers-Writers Problem

A

There’s a database with many readers and writers. When a reader is accessing it, there could be other readers. When a writer is accessing, there can’t be any other reader or writer.

22
Q

Dining Philosophers Problem

A

Philosophers alternate between thinking and eating, when eating they need both chopsticks, but a philosopher can only pick up one at a time. After eating, the philosopher puts down both chopsticks. This problem can lead to a DEADLOCK.

23
Q

Deadlocks

A

A set of processes is deadlocked when each process in the set is waiting for an event that only another process in the set can cause.

24
Q

Potential Deadlock Events

A

Waiting for a critical section, waiting for a condition to change, waiting for a physical resource.

25
Q

Conditions for a deadlock

A

Mutual exclusion, Hold and wait, No preemption, Circular wait.

26
Q

Hold and wait

A

A process needs to be holding at least one resource and waiting for at least one resource held by another.

27
Q

Circular wait

A

A set of processes must exist so that P1 waits on P2, P2 on P3, etc…

28
Q

Resource Allocation Graph

A

Vertices are processes and resources, edges are assignments and requests. For every resource, there can be many instances, and a requesting process can be granted if any of them are available.

29
Q

Handling Deadlocks

A

We could ignore them, detect and recover, avoid them, or prevent them.

30
Q

Deadlock Prevention

A

We can configure this at Hold and Wait, where at most one resource can be held/requested at any time; and ensure all requests are made at the same time. We could also use circular wait, and make sure requests are made in increasing or decreasing order, and make sure we are never holding a lower numbered resource when requesting a higher numbered one.

31
Q

Deadlock Avoidance

A

Avoid an action that leads to a deadlock, make sure we only transition from one safe state to another. Conservative approach.

32
Q

Safe State

A

Calculate allocable resources and required resources by a sequence of processes to determine if the state is safe. This is called a reduction sequence.

33
Q

Unsafe State

A

If there are less free units of a resource than number of processes in a sequence, the transition to that state will not occur by design.

34
Q

Banker’s Algorithm for Deadlock Avoidance

A

When a request is made, we check to see if there is at least one sequence of moves that can lead to a safe state, If one does not exist, we make the request wait.

35
Q

Deadlock Detection and Recovery

A

If there is only one instance of each resource, then a cycle in the allocation graph is sufficient to detect a deadlock.

36
Q

What to do when we detect a deadlock?

A

Preempt the resources, kill the processes, and checkpoint processes periodically, returning to the previous checkpoint on detection.