Docsity

Deadlocks and Starvation: Understanding Thread Synchronization Issues, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
Deadlocks and Starvation
Readings: Chapter on Deadlocks in Tanenbaum. You can skip Deadlock
Detection and Banker’s Algorithm.
• We looked at solving synchronization problems using monitors and
semaphores.
• Unfortunately, problems can arise
Example 1:
Recall our solution to the reader-writer problem. It was possible for
readers to wait indefinitely if new writers kept coming in.
On the other hand, writers would not wait indefinitely as long as ready
threads are served in order.
Why?
Example 2:
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Deadlocks and Starvation: Understanding Thread Synchronization Issues and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Deadlocks and Starvation

Readings:

(^) Chapter on Deadlocks in Tanenbaum. You can skip Deadlock

  • Detection and Banker’s Algorithm. semaphores.We looked at solving synchronization problems using monitors and

Unfortunately, problems can arise

Example 2: Why?threads are served in order.On the other hand, writers would not wait indefinitely as long as readyreaders to wait indefinitely if new writers kept coming in.Recall our solution to the reader-writer problem. It was possible forExample 1:

Thread A

Thread B

Unlock(x)Unlock(y)Use resource X and Ylock(y) lock(x)

Unlock(y)Unlock(x)Use resource X and YLock(x)Lock(y)

Common aspect: What is common and different between the above examples?indefinitely for each other, with no progress being made. In this case, it is possible that both Thread A and Thread B wait

(^) Both problems involve threads

(^) waiting

(^) for (^) resources to

become available. They can also involve

Resources:

(^) things needed by a thread to do its job a thread waits

o for resources waiting writers), disk blocks, memory pagese.g., locks, AW+WW = 0 (i.e., database is free from active or

Indefinite wait:

(^) In both examples, a thread may end up waiting

indefinitely.

Differences between the two examples:

(^) The type of waiting is different.

  • Avoiding starvation: If a deadlock does happen, is it a serious event?assuming Thread 1 starts out first?What is the sequence of events that must occur for a deadlock to happen,probability event or a high-probability event?Realistically, on any one run of the two threads, is a deadlock a low- priority.Switch priorities so that every thread has a chance to have high o will end up alternating.give priority to waiting readers. When both are waiting, theyE.g., Readers give priority to waiting writers, but active writers

o Raise priority if a thread has been waiting too long.

o Use FIFO order among competing requests

Preventing deadlocks:

To understand how to prevent deadlocks, one must figure out

necessary conditions for a deadlock to occur. There are four necessary

condition fails, then you cannot have a deadlock.conditions, all of which must hold, for a deadlock to occur. If any one 1) there's no waiting. i.e., not enough to serve all threads simultaneously. Otherwise Limited (shared) resource

No preemption:

(^) Preemption means you forcefully take away the

resource from someone. (Hard to do that with locks.)

Hold and wait:

(^) Threads in a deadlock hold a resource, and wait for

another resource

Circular wait:

o thread A is waiting for resource y

o resource y is held by thread B

o thread B is waiting for resource x

o resource x is held by thread A

The above is called a

(^) wait-for

(^) graph

  • e.g. both EECS 484 and EECS 482 are fullExample 3: circular waiting for resourcesIf they are not independent, why list all of them?Are the conditions independent? Or does one condition imply another? that you end up in either 484 or 482you want to switch from EECS 484 to EECS 482, but want to be sure

someone else wants to switch from EECS 482 to EECS 484 :-)

Example #4: Deadlock always leads to starvation (but not necessarily vice versa)The shared resources in this case is spot in the two classes.Both of you are waiting for each other to drop the course, will wait forever(don't want to drop the old course and not be able to add the new course)To switch, you want to add the new course, then drop the old course

(^) dining philosophers

each philosopher needs two chopsticks to eatchopsticks total)5 philosphers sitting around a round table, 1 chopstick in between each (5^ (This is a classic problem, also due to Dijkstra) Algorithm for philosopher:

put both chopsticks downeatwait for left chopstick to be free, then pick it upwait for right chopstick to be free, then pick it up

Can this deadlock? If so, how?

undo the firing of a missile?Not always possible to roll back a thread. E.g., how do you

Basic idea is to get rid of one of the 4 necessary conditions Deadlock prevention (and avoidance):

  • Make resources unlimited (or at least enough that there's no waiting) Frequently impossible, but this is the best solution

have full classes, but unlikelymake it very unlikely. E.g., large classrooms. Still possible toEven if you can't make it so deadlock is impossible, you can

Dining Philosophers:reduce the number of threads competing for them.Sometimes, if you cannot make resources unlimited, you can

  1. Allow preemption

switch, get back to it later)can preempt CPU (save its state in its thread control block and

back latercan preempt memory, e.g., by saving it to disk and loading it

can't preempt the holding of a lock

of a critical section.is dangerous to preempt locks. A thread may be in the middleUsually not applicable to synchronization problems because it

  1. Eliminate hold and wait

California, you get a busy signal.E.g., phone company. If it can't get all the lines needed to callEliminate hold: if can't satisfy resource right away, thread dies.

Example 2?Time-out requests for each lock and retry? Will this work in still held. This is sometime called aThis is just another form of waiting if some resources are

(^) livelock

way, giving up all the held resources, and retry.On a time-out, threads must be prepared to back out all the

could be viewed as undoing its recent actions)chopstick. If the other is busy, drop the one you grabbed. Thisbeginning. E.g., grab both chopsticks at same time (or grab oneEliminate wait while holding: Request all resources at o all resources at beginningmany resources need. Inefficient, because it reserveshard to predict the future. Tend to overestimate how

Circular chain of requests Impose an ordering between resources and ensure that you

always request resources in order.