A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

Video Capítulo 8: Impasses. Silberschatz, Galvin and Gagne  2002 3.2 Video Operating System Concepts Assuntos n Modelo de Sistemas n Carcterização de.

Apresentações semelhantes


Apresentação em tema: "Video Capítulo 8: Impasses. Silberschatz, Galvin and Gagne  2002 3.2 Video Operating System Concepts Assuntos n Modelo de Sistemas n Carcterização de."— Transcrição da apresentação:

1 Video Capítulo 8: Impasses

2 Silberschatz, Galvin and Gagne  2002 3.2 Video Operating System Concepts Assuntos n Modelo de Sistemas n Carcterização de impasses n Métodos para Evitar impasses n Prevenção de impasses n Detecção de impasses n Recuperação de impasse n Combinando Abordagens

3 Silberschatz, Galvin and Gagne  2002 3.3 Video Operating System Concepts Impasse n Conjunto de processos bloqueados n Cada processo necessita de um recurso utilizado por outro processo do conjunto

4 Silberschatz, Galvin and Gagne  2002 3.4 Video Operating System Concepts Exemplo de Impasse n Sistema com duas unidades de fita F Processos P 1 e P 2 4 cada um como uma unidade e precisando da outra. F P0 P1 F Espera(A); Espera(B) F Espera(B); Espera(A)

5 Silberschatz, Galvin and Gagne  2002 3.5 Video Operating System Concepts Exemplo da Ponte n Tráfego em uma direção n Cada parte da ponte F um recurso. n Se um impasse ocorre F pode ser resolvido se um carro voltar n Muitos carros podem ter que voltar F se ocorrer um impasse

6 Silberschatz, Galvin and Gagne  2002 3.6 Video Operating System Concepts Modelo n Classes de recursos R 1, R 2,..., R m F Ciclos de CPU, Espaço de Memória,Dispositivos de E/S) n Cada recurso R i tem W i instâncias. n Cada processo segue a seguinte sequência para utilizar um recurso F requisição F uso F liberação

7 Silberschatz, Galvin and Gagne  2002 3.7 Video Operating System Concepts Caracterização Para ocorrer um impasse quatro condições devem ser satisfeitas: F Exclusão Mútua F “Pega e espera” F Inexistência de Preempção F Espera Circular

8 Silberschatz, Galvin and Gagne  2002 3.8 Video Operating System Concepts Exclusão Mútua n Apenas um processo pode usar o recurso a cada instante n Ex: impressora F Se dois processos imprimem simultaneamente, o saída é embaralhada

9 Silberschatz, Galvin and Gagne  2002 3.9 Video Operating System Concepts “Pega e Espera” n Hold and Wait n Um processo que já “pegou” pelo menos um recurso espera por outro recurso que está alocado para outro processo

10 Silberschatz, Galvin and Gagne  2002 3.10 Video Operating System Concepts Inexistência de Preempção n Um recurso só poder ser liberado voluntáriamente pelo processo que o possui, depois de ter completado sua tarefa

11 Silberschatz, Galvin and Gagne  2002 3.11 Video Operating System Concepts Espera Circular n Existe um conjunto {P0, P1,..., Pn} de processos tais que P0 está esperando por um recurso que está alocado para P1, P(x-1) está esperando por um recurso alocado para P(x), e Pn espera por um recurso alocado para P0

12 Silberschatz, Galvin and Gagne  2002 3.12 Video Operating System Concepts Grafo de Alocação de Recursos n Um conjunto de vértices V e arestas A n V é particionado em dois tipos: F P = {P1, P2,..., Pn}, o conjuto de todos os processos no sistema F R={R1, R2,..., Rn} o conjunto de todos os tipos de recursos no sistema n Aresta de pedido F Aresta direcionada Pi -> Ri n Aresta de alocação F Aresta direcionada Ri -> Pi

13 Silberschatz, Galvin and Gagne  2002 3.13 Video Operating System Concepts Grafo de Alocação de Recursos (cont) n Processo n Recurso com 4 instâncias n Pi requisita instância de Rj n Pi “pegou” uma instância de Rj PiPi PiPi RjRj RjRj

14 Silberschatz, Galvin and Gagne  2002 3.14 Video Operating System Concepts Example of a Resource Allocation Graph

15 Silberschatz, Galvin and Gagne  2002 3.15 Video Operating System Concepts Resource Allocation Graph With A Deadlock

16 Silberschatz, Galvin and Gagne  2002 3.16 Video Operating System Concepts Resource Allocation Graph With A Cycle But No Deadlock

17 Silberschatz, Galvin and Gagne  2002 3.17 Video Operating System Concepts Basic Facts n If graph contains no cycles  no deadlock. n If graph contains a cycle  F if only one instance per resource type, then deadlock. F if several instances per resource type, possibility of deadlock.

18 Silberschatz, Galvin and Gagne  2002 3.18 Video Operating System Concepts Methods for Handling Deadlocks n Ensure that the system will never enter a deadlock state. n Allow the system to enter a deadlock state and then recover. n Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX.

19 Silberschatz, Galvin and Gagne  2002 3.19 Video Operating System Concepts Deadlock Prevention n Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources. Restrain the ways request can be made.

20 Silberschatz, Galvin and Gagne  2002 3.20 Video Operating System Concepts Deadlock Prevention (Cont.) n Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources. F Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none. F Low resource utilization; starvation possible.

21 Silberschatz, Galvin and Gagne  2002 3.21 Video Operating System Concepts Deadlock Prevention (Cont.) n No Preemption – F If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released. F Preempted resources are added to the list of resources for which the process is waiting. F Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.

22 Silberschatz, Galvin and Gagne  2002 3.22 Video Operating System Concepts Deadlock Prevention (Cont.) n Circular Wait – F impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.

23 Silberschatz, Galvin and Gagne  2002 3.23 Video Operating System Concepts Deadlock Avoidance n Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need. n The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition. n Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes. Requires that the system has some additional a priori information available.

24 Silberschatz, Galvin and Gagne  2002 3.24 Video Operating System Concepts Safe State n When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state. n System is in safe state if there exists a safe sequence of all processes.

25 Silberschatz, Galvin and Gagne  2002 3.25 Video Operating System Concepts Safe State n Sequence is safe if for each P i, the resources that Pi can still request can be satisfied by currently available resources + resources held by all the P j, with j<I.

26 Silberschatz, Galvin and Gagne  2002 3.26 Video Operating System Concepts Safe State F If P i resource needs are not immediately available, then P i can wait until all P j have finished. F When P j is finished, P i can obtain needed resources, execute, return allocated resources, and terminate. F When P i terminates, P i+1 can obtain its needed resources, and so on.

27 Silberschatz, Galvin and Gagne  2002 3.27 Video Operating System Concepts Basic Facts n If a system is in safe state  no deadlocks. n If a system is in unsafe state  possibility of deadlock. n Avoidance  ensure that a system will never enter an unsafe state.

28 Silberschatz, Galvin and Gagne  2002 3.28 Video Operating System Concepts Safe, Unsafe, Deadlock State

29 Silberschatz, Galvin and Gagne  2002 3.29 Video Operating System Concepts Resource-Allocation Graph Algorithm n Claim edge P i  R j indicated that process P j may request resource R j ; represented by a dashed line. n Claim edge converts to request edge when a process requests a resource. n When a resource is released by a process, assignment edge reconverts to a claim edge. n Resources must be claimed a priori in the system.

30 Silberschatz, Galvin and Gagne  2002 3.30 Video Operating System Concepts Resource-Allocation Graph For Deadlock Avoidance

31 Silberschatz, Galvin and Gagne  2002 3.31 Video Operating System Concepts Unsafe State In Resource-Allocation Graph

32 Silberschatz, Galvin and Gagne  2002 3.32 Video Operating System Concepts Banker’s Algorithm n Multiple instances. n Each process must a priori claim maximum use. n When a process requests a resource it may have to wait. n When a process gets all its resources it must return them in a finite amount of time.

33 Silberschatz, Galvin and Gagne  2002 3.33 Video Operating System Concepts Data Structures for the Banker’s Algorithm n Available n Max n Allocation: n Need: Let n = number of processes, and m = number of resources types.

34 Silberschatz, Galvin and Gagne  2002 3.34 Video Operating System Concepts Available n Available: Vector of length m. If available [j] = k, there are k instances of resource type R j available.

35 Silberschatz, Galvin and Gagne  2002 3.35 Video Operating System Concepts Max n Max: n x m matrix. If Max [i,j] = k, then process P i may request at most k instances of resource type R j.

36 Silberschatz, Galvin and Gagne  2002 3.36 Video Operating System Concepts Allocation n Allocation: n x m matrix. If Allocation[i,j] = k then P i is currently allocated k instances of R j.

37 Silberschatz, Galvin and Gagne  2002 3.37 Video Operating System Concepts Need n Need: n x m matrix. If Need[i,j] = k, then P i may need k more instances of R j to complete its task. Need [i,j] = Max[i,j] – Allocation [i,j].

38 Silberschatz, Galvin and Gagne  2002 3.38 Video Operating System Concepts Safety Algorithm 1.Let Work and Finish be vectors of length m and n, respectively. Initialize: Work = Available Finish [i] = false for i - 1,3, …, n. 2.Find and i such that both: (a) Finish [i] = false (b) Need i  Work If no such i exists, go to step 4. 3.Work = Work + Allocation i Finish[i] = true go to step 2. 4.If Finish [i] == true for all i, then the system is in a safe state.

39 Silberschatz, Galvin and Gagne  2002 3.39 Video Operating System Concepts Resource-Request Algorithm for Process P i Request = request vector for process P i. If Request i [j] = k then process P i wants k instances of resource type R j. 1.If Request i  Need i go to step 2. Otherwise, raise error condition, since process has exceeded its maximum claim. 2.If Request i  Available, go to step 3. Otherwise P i must wait, since resources are not available. 3.Pretend to allocate requested resources to P i by modifying the state as follows: Available = Available = Request i ; Allocation i = Allocation i + Request i ; Need i = Need i – Request i;; If safe  the resources are allocated to P i. If unsafe  P i must wait, and the old resource-allocation state is restored

40 Silberschatz, Galvin and Gagne  2002 3.40 Video Operating System Concepts Example of Banker’s Algorithm n 5 processes P 0 through P 4 ; 3 resource types A (10 instances), B (5instances, and C (7 instances). n Snapshot at time T 0 : AllocationMaxAvailable A B CA B C A B C P 0 0 1 07 5 3 3 3 2 P 1 2 0 0 3 2 2 P 2 3 0 2 9 0 2 P 3 2 1 1 2 2 2 P 4 0 0 24 3 3

41 Silberschatz, Galvin and Gagne  2002 3.41 Video Operating System Concepts Example (Cont.) n The content of the matrix. Need is defined to be Max – Allocation. Need A B C P 0 7 4 3 P 1 1 2 2 P 2 6 0 0 P 3 0 1 1 P 4 4 3 1 n The system is in a safe state since the sequence satisfies safety criteria.

42 Silberschatz, Galvin and Gagne  2002 3.42 Video Operating System Concepts Example P 1 Request (1,0,2) (Cont.) n Check that Request  Available (that is, (1,0,2)  (3,3,2)  true. AllocationNeedAvailable A B CA B CA B C P 0 0 1 0 7 4 3 2 3 0 P 1 3 0 20 2 0 P 2 3 0 1 6 0 0 P 3 2 1 1 0 1 1 P 4 0 0 2 4 3 1 n Executing safety algorithm shows that sequence satisfies safety requirement. n Can request for (3,3,0) by P 4 be granted? n Can request for (0,2,0) by P 0 be granted?

43 Silberschatz, Galvin and Gagne  2002 3.43 Video Operating System Concepts Deadlock Detection n Allow system to enter deadlock state n Detection algorithm n Recovery scheme

44 Silberschatz, Galvin and Gagne  2002 3.44 Video Operating System Concepts Single Instance of Each Resource Type n Maintain wait-for graph F Nodes are processes. F P i  P j if P i is waiting for P j. n Periodically invoke an algorithm that searches for a cycle in the graph. n An algorithm to detect a cycle in a graph requires an order of n 2 operations, where n is the number of vertices in the graph.

45 Silberschatz, Galvin and Gagne  2002 3.45 Video Operating System Concepts Resource-Allocation Graph and Wait- for Graph Resource-Allocation GraphCorresponding wait-for graph

46 Silberschatz, Galvin and Gagne  2002 3.46 Video Operating System Concepts Several Instances of a Resource Type n Available: A vector of length m indicates the number of available resources of each type. n Allocation: An n x m matrix defines the number of resources of each type currently allocated to each process. n Request: An n x m matrix indicates the current request of each process. If Request [i j ] = k, then process P i is requesting k more instances of resource type. R j.

47 Silberschatz, Galvin and Gagne  2002 3.47 Video Operating System Concepts Detection Algorithm 1.Let Work and Finish be vectors of length m and n, respectively Initialize: (a) Work = Available (b)For i = 1,2, …, n, if Allocation i  0, then Finish[i] = false;otherwise, Finish[i] = true. 2.Find an index i such that both: (a)Finish[i] == false (b)Request i  Work If no such i exists, go to step 4.

48 Silberschatz, Galvin and Gagne  2002 3.48 Video Operating System Concepts Detection Algorithm (Cont.) 3.Work = Work + Allocation i Finish[i] = true go to step 2. 4.If Finish[i] == false, for some i, 1  i  n, then the system is in deadlock state. Moreover, if Finish[i] == false, then P i is deadlocked. Algorithm requires an order of O(m x n 2) operations to detect whether the system is in deadlocked state.

49 Silberschatz, Galvin and Gagne  2002 3.49 Video Operating System Concepts Example of Detection Algorithm n Five processes P 0 through P 4 ; three resource types A (7 instances), B (2 instances), and C (6 instances). n Snapshot at time T 0 : AllocationRequestAvailable A B C A B C A B C P 0 0 1 0 0 0 0 0 0 0 P 1 2 0 0 2 0 2 P 2 3 0 30 0 0 P 3 2 1 1 1 0 0 P 4 0 0 2 0 0 2 n Sequence will result in Finish[i] = true for all i.

50 Silberschatz, Galvin and Gagne  2002 3.50 Video Operating System Concepts Example (Cont.) n P 2 requests an additional instance of type C. Request A B C P 0 0 0 0 P 1 2 0 1 P 2 0 0 1 P 3 1 0 0 P 4 0 0 2 n State of system? F Can reclaim resources held by process P 0, but insufficient resources to fulfill other processes; requests. F Deadlock exists, consisting of processes P 1, P 2, P 3, and P 4.

51 Silberschatz, Galvin and Gagne  2002 3.51 Video Operating System Concepts Detection-Algorithm Usage n When, and how often, to invoke depends on: F How often a deadlock is likely to occur? F How many processes will need to be rolled back? 4 one for each disjoint cycle n If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes “caused” the deadlock.

52 Silberschatz, Galvin and Gagne  2002 3.52 Video Operating System Concepts Recovery from Deadlock: Process Termination n Abort all deadlocked processes. n Abort one process at a time until the deadlock cycle is eliminated. n In which order should we choose to abort? F Priority of the process. F How long process has computed, and how much longer to completion. F Resources the process has used. F Resources process needs to complete. F How many processes will need to be terminated. F Is process interactive or batch?

53 Silberschatz, Galvin and Gagne  2002 3.53 Video Operating System Concepts Recovery from Deadlock: Resource Preemption n Selecting a victim – minimize cost. n Rollback – return to some safe state, restart process for that state. n Starvation – same process may always be picked as victim, include number of rollback in cost factor.

54 Silberschatz, Galvin and Gagne  2002 3.54 Video Operating System Concepts Combined Approach to Deadlock Handling n Combine the three basic approaches F prevention F avoidance F detection allowing the use of the optimal approach for each of resources in the system. n Partition resources into hierarchically ordered classes. n Use most appropriate technique for handling deadlocks within each class.

55 Silberschatz, Galvin and Gagne  2002 3.55 Video Operating System Concepts Traffic Deadlock for Exercise 8.4


Carregar ppt "Video Capítulo 8: Impasses. Silberschatz, Galvin and Gagne  2002 3.2 Video Operating System Concepts Assuntos n Modelo de Sistemas n Carcterização de."

Apresentações semelhantes


Anúncios Google