deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · detection-algorithm usage 1. how...

20
Deadlocks pp. 309

Upload: others

Post on 12-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Deadlocks

pp. 309

Page 2: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

System Model1. Request2. Use3. Release

Necessary Conditions1. Mutual exclusion (non-sharable)

2. Hold and wait (holding at least one resource, and wait)

3. No preemption (resources cannot be preempted)

4. Circular wait

A process may utilize a resource in only the following sequence:

เปรยบเทยบกบไฟ ทม necessary conditionคอ ความรอน เชอเพลง และอากาศ

necessary condition ของ event A หมายถงถาพบ event A จะตองพบ necessary cond ดวยแตพบ necessary cond อาจจะไมเกด event A กไดเชน ถานกบนได นกตองมปก แตมปกอาจจะบนไมได (เพนกวน)

Page 3: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Resource-Allocation Graph

allocated

wait

Page 4: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Resource-Allocation Graph

Necessary but not sufficient!

Page 5: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Methods for Handling Deadlocks1. Deadlock prevention

Prevent at least one of the necessary conditions.

2. Deadlock avoidanceInformation in advance (wait/no wait requests).

3. Deadlock detection and recoveryDeadlocks arise, detected, and recovered.

4. Do nothingPerformance deterioration, stop functioning, and need to be manually restarted.

จะไมพยายามไปแกnecessary condition

Page 6: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Deadlock Prevention1. Mutual exclusion

Intrinsically non-sharable.

2. Hold and waitRequest resources only when having none.

3. No preemptionIf a request fails, all resources are preempted.

4. Circular waitRequest resources in increasing order.

Read-only file (sharable)Memory (non-sharable)

Whenever a process requests a resource,it does not hold any other resources.

2.1 Request all resources at the beginning (low utilization)2.2 Release all resources before making a request (cannot always release)Example copy file from DVD to disk, sort, and print.A process that needs several popular resources may have to wait indefinitely (starvation).

Often applied to resources whose state can be easily saved and restored later.

CPU registersand memory.

Page 7: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

F(tape drive) = 1

F(disk drive) = 5

F(printer) = 12

Each process can request resources only in an increasing order.

1. Process can request Rj that F(Rj) > F(Ri)

2. If requesting Rj, process must have released any Ri thatF(Ri) ≥ F(Rj).

It must also be noted that if several instances of the same resource type are needed, a single request for all of them must be issued.

If these two protocols are used, then the circular-wait condition cannot hold (proof by contradiction).

Page 8: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

pp. 294

void transaction(Account from, Account to, double amount) {

Semaphore lock1, lock2;

lock1 = getLock(from);

lock2 = getLock(to);

wait(lock1);

wait(lock2);

withdraw(from, amount);

deposit(to, amount);

signal(lock1);

signal(lock2);

}

Transaction(AccX, AccY, 25);

Transaction(AccY, AccX, 50);

AccX

AccY

It is also important to note that imposing a lock ordering does not guarantee deadlock prevention if locks can be acquired dynamically.

Keep in mind that developing an ordering, or hierarchy, does not in itself prevent deadlock. It is up to application developers to write programs that follow the ordering.

How to correct the program to solve deadlock?

Page 9: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Deadlock Avoidance

Page 10: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

pp. 295

Page 11: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Total 12 tape drives.Safe sequence: <P1, P0, P2>

Suppose that P2 requests 1 more tape drive and is allocated.System is no longer in safe state.

Deadlock may occur, for instance,P1 is allocated and returns all tape drives

(12 – 5 – 0 – 3) = 4 tapes are available).P0 requests 5 and P2 requests 6 tape drives.Deadlock!

4 tapes

P0 P2

5 6

3

Page 12: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Resource-Allocation-Graph Algorithm

pp. 297

Cycle-detection algorithmO(n2) where n is number of processes.

Go ahead

Avoid it

วธนใชกบ resource ทมmultiple instance ไมได !!!

Page 13: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Banker’s Algorithmn number of processesm number of resource typesAvailable vector of length m (number of available instances)Max n x m matrix (maximum demand)Allocation n x m matrix (number of allocated instances)Need n x m matrix (remaining resource need)

Page 14: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Safety? Algorithm1. Work = Available // vector of length m

Finish = (false, false, …, false) // vector of length n

2. Find an index i such that botha. Finish[i] = falseb. Needi ≤ Work

3. Work = Work + AllocationiFinish[i] = true;Goto step 2

4. If Finish[i] == true for all i, then the system is in safe state.

Time complexity = O(mn2) สงกวา cycle detection algorithm

Main idea:ไลหา safe sequence

true คอ process ท างานเสรจ, false คอยง

Page 15: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Resource-Request Algorithm pp. 299

P1 requests (1, 0, 2) and granted.

Safe (ม safe seq > 1)<P1,P3,P4,P2,P0>

Safe, then granted<P1,P3,P4,P0,P2>

P4 requests (3, 3, 0) not enough available resources

P0 requests (0, 2, 0) unsafe

1

3

2

4

Main idea:เมอม request กลองสมมตวาใหดกอนแลวรน safety algo วา safe หรอไม

Page 16: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Deadlock Detection• Single instance

Resource-allocation graph called wait-for graph.

• Several instanceDeadlock-detection algorithm (similar to Safety algo).

Cycle = Deadlock

Page 17: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Deadlock-Detection Algorithm1. Work = Available // vector of length m

Finish = (true, false, … true) // vector of length nfalse if Allocationi ≠ 0

2. Find an index i such that botha. Finish[i] = falseb. Requesti ≤ Work

3. Work = Work + AllocationiFinish[i] = true;Goto step 2

4. If Finish[i] == false for some i, 0 ≤ i < n, then Pi is deadlocked.

Time complexity = O(mn2)

Main idea:สมมตกรณ best caseวาทก process จะไม request เพมพยายามไลใหทก process ท างานเสรจถาท าไมได กเกด deadlock แลว

Page 18: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

pp. 303

No deadlock

Deadlock!

ไมตองด max หรอ needcurrent request

Page 19: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

Detection-Algorithm Usage

1. How often is a deadlock likely to occur?2. How many processes will be effected by deadlock when it happens?

Recovery from Deadlock

1. Process TerminationAbort all deadlocked processes.Abort one process at a time until the deadlock cycle is eliminated.

2. Resource PreemptionSelecting a victim.Rollback (due to resource preempted, total rollback = restart).Starvation (re-preempt from the same process over and over).

Page 20: Deadlockspioneer.netserv.chula.ac.th/~achatcha/2301371/07.pdf · Detection-Algorithm Usage 1. How often is a deadlock likely to occur? 2. How many processes will be effected by deadlock

https://www.quora.com/Which-method-handles-deadlock-from-Linux-Unix-Windows-7-and-10

ใน database แก deadlock ได เพราะ rollback หรอ undo ได ก restart process ทท าใหเกด deadlock ออกไปกอน

แลวใน database เกด deadlock ไดยงไง ?