Download presentation

Published byWarren Garrett Modified over 3 years ago

1
**Synchronization Algorithms and Concurrent Programming**

Chapter 1 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld Chapter 7 Multiple resources The dinning philosophers problem Version: June 2014 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

2
**Synchronization Algorithms and Concurrent Programming**

Chapter 1 Synchronization Algorithms and Concurrent Programming ISBN: , 1st edition A note on the use of these ppt slides: I am making these slides freely available to all (faculty, students, readers). They are in PowerPoint form so you can add, modify, and delete slides and slide content to suit your needs. They obviously represent a lot of work on my part. In return for use, I only ask the following: That you mention their source, after all, I would like people to use my book! That you note that they are adapted from (or perhaps identical to) my slides, and note my copyright of this material. Thanks and enjoy! Gadi Taubenfeld All material copyright 2014 Gadi Taubenfeld, All Rights Reserved To get the most updated version of these slides go to: Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

3
**2.4 The Dining Philosophers 2.5 Hold and Wait Strategy **

Chapter 1 Chapter 7 Multiple Resources 2.1 Deadlocks 2.2 Deadlock Prevention 2.3 Deadlock Avoidance 2.4 The Dining Philosophers 2.5 Hold and Wait Strategy 2.5 Wait and Release Strategy 2.6 Randomized algorithms Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2006

4
**Deadlocks Section 7.1 Chapter 7**

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

5
Deadlocks A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

6
**Multiple resources How to avoid deadlock?**

account A account B Transferring money between two bank accounts P P1 down(A); down(B) down(B); down(A) semaphores A and B, initialized to 1 deadlock Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

7
**Multiple resources How to avoid deadlock?**

Bridge crossing On the bridge traffic only in one direction. The resources are the two entrances. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

8
Two Simple Questions Question: A system has 2 processes and 3 identical resources. Each process needs a maximum of 2 resources. Is deadlock possible? No Question: Consider a system with X identical resources. The system has 15 processes each needing a maximum of 15 resources. What is the smallest value for X which makes the system deadlock-free (without the need to use a deadlock avoidance algorithm)? 15×14+1 = 211 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

9
Question Question: Two processes, P1 and P2 each need to hold five records 1,2,3,4 and 5 in a database to complete. If P1 asks for them in the order 1,2,3,4,5 and P2 asks them in the same order, deadlock is not possible. However, if P2 asks for them in the order 5,4,3,2,1 then deadlock is possible. With five resources, there are 5! or 120 possible combinations each process can request the resources. Hence there are 5!×5! different algorithms. What is the exact number of algorithms (out of 5!×5!) that is guaranteed to be deadlock free? 5!(4!×4!) = (5!×5!)/5 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

10
**Strategies for dealing with Deadlocks**

Just ignore the problem altogether UNIX and Windows take this approach. Detection and recovery Allow the system to enter a deadlock state and then recover. Avoidance By careful resource allocation, ensure that the system will never enter a deadlock state. Prevention The programmer should write programs that never deadlock. This is achieved by negating one of the four necessary conditions for deadlock to occur (mentioned in the next slide.) Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

11
**Deadlock Prevention Section 7.2 Chapter 7**

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

12
**Deadlock Prevention Attacking one of the following conditions for deadlock**

Mutual exclusion condition one process at a time can use the resource. Hold and wait condition a process can request (and wait for) a resource while holding another resource. No preemption condition A resource can be released only voluntarily by the process holding it. Circular wait condition must be a cycle involving several processes, each waiting for a resource held by the next one. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

13
**Attacking the mutual exclusion condition**

Attacking the no preemption condition Some devices (such as printer) can be spooled only the printer daemon uses printer resource, thus deadlock for printer eliminated Not all devices can be spooled attack is not useful in general Many resources (such as printer) should not be preempted can not take the printer from a process that has not finished printing yet attack is not useful in general Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

14
**Attacking the Hold and Wait Condition**

Processes may request all the resources they need in advance. Problems May not know all required resources in advance. Inefficient : ties up resources other processes could be using. Starvation is possible. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

15
**Two-Phase Locking (Notice similarity to requesting all resources at once)**

Phase one The process tries to lock all the resources it currently needs, one at a time if needed record is not avaliable, release and start over Phase two: when phase one succeeds, performing updates releasing locks “livelock” is possible. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

16
**The time-stamping ordering technique**

Time stamps: Before a process starts locking a unique new timestamp is associated with that process. If a process has been assigned timestamp Ti and later a new process has assigned timestamp Tj then Ti <Tj. We associate with each resource a timestamp value, which is the timestamp of the process that is currently holding that resource. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

17
**The time-stamping ordering technique**

Phase one: the process tries to lock all the resources it currently needs, one at a time. If a needed resource is not available and the timestamp value is smaller than that of the process, release all the resources, waits until the resource with the smaller timestamp is released, and starts over. Otherwise, if the timestamp of the resource is not smaller, waits until the resource is released and locks it. Phase two: when phase one succeeds, performing updates; releasing locks. Prevents deadlock and starvation. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

18
**Attacking the Circular Wait Condition**

Impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. 1 2 3 4 5 6 7 time account A account B Solves transferring money between two bank accounts We will see other interesting usage of this observation Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

19
**Deadlock Avoidance Section 7.3 Chapter 7**

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

20
**Deadlock Avoidance Safe and Unsafe States safe unsafe deadlock**

time All terminated! Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

21
**Basic Facts If a system is in safe state no deadlock.**

If a system is in unsafe state deadlock now or in the future. Deadlock Avoidance ensure that a system will never enter an unsafe state. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

22
**Example: Prove that the state below is safe**

Allocation Maximum P1 1 9 P2 4 5 P3 2 8 available : 2 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

23
**Proof time P1 1 9 P1 1 9 P1 1 9 P1 1 9 P1 1 9 P2 4 5 P2 5 5 P2 - P2 -**

Allocation Allocation Allocation Allocation Allocation Maximum Maximum Maximum Maximum Maximum P1 1 9 P1 1 9 P1 1 9 P1 1 9 P1 1 9 P2 4 5 P2 5 5 P2 - P2 - P2 - P3 2 8 P3 2 8 P3 2 8 P3 8 8 P3 - available : 2 available : 1 available : 6 available : 0 available : 8 P1 9 9 P1 -- P2 -- P2 -- P3 -- P3 -- available : 0 available : 9 time Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

24
**Example: safe and unsafe**

Allocation Maximum P1 1 9 If process P1 requests one (out of the 2 avaliable), resources the Banker will not allocated it. P2 4 5 P3 2 8 available : 2 P1 2 9 unsafe state P2 4 5 P3 2 8 available : 1 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

25
**The Banker’s Algorithm**

When there is a request for an available resource, the banker must decide if immediate allocation leaves the system in a safe state. If the answer is positive, the resource is allocated, otherwise the request is temporarily denied. A state is safe if there exists a sequence of all processes <P1, P2, …, Pn> such that for each Pi, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj, with j < i. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

26
**The Banker’s Algorithm: commensts**

Can handle Multiple instances. Each process must a priori claim maximum use -- a disadvantage. When a process requests a resource it may have to wait. When a process gets all its resources it must return them in a finite amount of time. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

27
**The Dinning Philosophers Problem**

Section 7.4 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

28
**Dining Philosophers Philosophers think take forks eat put forks**

Eating needs 2 forks Pick one fork at a time How to prevent deadlock Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

29
**An incorrect solution L L L L L L ( means “waiting for this forks”)**

Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

30
**An inefficient solution**

using mutual exclusion Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

31
**Proving deadlock-freedom & starvation-freedom**

Impose a total ordering of all forks, and require that each philosopher requests resources in an increasing order. 1 2 R L R 6 3 R R R 5 4 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

32
**The Hold and Wait Strategy**

Section 7.5 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

33
**The LR Solution L R R L L R Chapter 7**

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

34
**Proving deadlock-freedom & starvation-freedom**

The LR Solution Proving deadlock-freedom & starvation-freedom 1 4 R L L 6 2 R R L 3 5 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

35
**Concurrency How many can eat simultaneously?**

Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

36
**At most half can eat simultaneously**

Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

37
**Only one third can eat simultaneously**

Any algorithm is at most n/3-concurrent Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

38
**In LR only one forth can eat simultaneously**

The LR algorithm is at most n/4-concurrent Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

39
LR If all want to eat, there is a case where only n/4 will be able to eat simultaneously. ( means “waiting for this forks”) R L L R free Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

40
**Robustness k-robust: if all except k consecutive**

philosophers fail, then one will not starve. Any algorithm is at most n/3-robust. The LR algorithm is not 4-robust. The LR algorithm is 5-robust iff n is even. There is no 4-robust algorithm using a hold and wait strategy. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

41
**The LLR Solution L R L L R L Chapter 7**

Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

42
**Proving deadlock-freedom & starvation-freedom**

LLR Proving deadlock-freedom & starvation-freedom 3 2 L R L 1 L R L 5 6 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

43
**In LLR one third can eat simultaneously**

The LLR algorithm is n/3-concurrent A tight bound Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

44
**Robustness k-robust: if all except k consecutive**

philosophers fail, then one will not starve. The LLR algorithm is not 4-robust. The LLR algorithm is 5-robust iff n 0 mod 3. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

45
**The Hold and Release Strategy**

Section 7.6 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

46
**The LR wait/release Algorithm**

The LR wait/release algorithm is: deadlock-free but not starvation-free. n/3-concurrent. 3-robust iff n is even. Recall: There is no 4-robust algorithm using a hold and wait strategy. L R R L L R Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

47
**Randomized Algorithms**

Section 7.7 Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

48
**Two Randomized Algorithms**

The Free Philosophers algorithm is: deadlock-free with probability 1, but is not starvation-free and is not 2-concurrent. 3-robust with probability 1. The Courteous Philosophers algorithm is: starvation-free with probability 1, but is not 2-concurrent and is not (n-1)-robust. Chapter 7 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014

Similar presentations

OK

Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks

Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google

Ppt on conservation of endangered species Ppt on producers consumers and decomposers video Ppt on human digestive system for class 7 Ppt on water cycle for class 7 Make a ppt on unity in diversity and organic farming Ppt on lateral thinking Ppt on micro controller traffic light Ppt on nature and human relationship Ppt on cell the basic unit of life Ppt on ideal gas law