MC504 Sistemas Operacionais
Prof. Dr. Eng. Isaías Bittencourt Felzmann
isaias@ic.unicamp.br
Campinas, 2s/2026
Deadlock
1/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Aviso Legal
The following set of slides are copyright Silberschatz, Galvin and Gagne, 2018. Modifications were made for their use in conjunction with MC504. The original material is available at os-book.com .
Os direitos autorais do conjunto de slides a seguir pertencem a Silberschatz, Galvin and Gagne, 2018. Foram feitas modificações para seu uso em MC504. O material original está disponível em os-book.com .
Este material foi elaborado com apoio de ferramentas de IA generativa como recurso auxiliar. O conteúdo foi revisado, validado e é de responsabilidade do docente.
2/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Deadlock!
3/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Objetivos do Capítulo
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
4/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Modelo de Sistema
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
5/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Deadlock com Semáforos
wait(s1)
wait(s2)
wait(s2)
wait(s1)
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
6/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Caracterização do deadlock
O deadlock pode ocorrer se as quatro condições forem satisfeitas simultaneamente.
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
7/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Grafo de Alocação de Recursos
Um conjunto de vértices V e um conjunto de arestas E.
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
8/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Exemplo de Grafo de Alocação de Recursos
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
9/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Grafo de Alocação de Recursos com um Deadlock
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
10/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Grafo com um ciclo, mas sem deadlock
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
11/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Fatos Básicos
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
12/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Métodos para lidar com deadlocks
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
13/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Prevenção de Deadlock
Invalidar uma das quatro condições necessárias para o deadlock:
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
14/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Prevenção de Deadlock (Continuação)
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
15/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Espera Circular
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
16/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Evitação de Deadlock
Exige que o sistema tenha alguma informação adicional a priori �disponível
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
17/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Estado Seguro
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
18/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Fatos Básicos
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
19/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Estados Seguro, Inseguro e de Deadlock
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
20/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Algoritmos de Evitação
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
21/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Grafo de Alocação de Recursos
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
22/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Grafo de Alocação de Recursos
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
23/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Estado inseguro no Grafo de Alocação de Recursos
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
24/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Algoritmo de Grafo de Alocação de Recursos
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
25/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Algoritmo do Banqueiro
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
26/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Estruturas de Dados para o Algoritmo do Banqueiro
�Need [i,j] = Max[i,j] – Allocation [i,j]
Seja n = número de processos, e m = número de tipos de recursos.
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
27/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Algoritmo de Segurança
Work = Available
Finish [i] = false para i = 0, 1, ..., n- 1
(a) Finish [i] = false
(b) Needi ≤ Work
Se não existir tal i , vá para o passo 4
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
28/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Algoritmo de Solicitação de Recursos para o Processo Pi
Requesti = vetor de solicitação para o processo Pi. Se Requesti [j] = k, então o processo Pi quer k instâncias do tipo de recurso Rj
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
29/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Exemplo do Algoritmo do Banqueiro
3 tipos de recursos:
A (10 instâncias), B (5 instâncias) e C (7 instâncias)
Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
30/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Exemplo (cont.)
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1 �
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
31/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Exemplo: Solicitação de P1 (1,0,2)
Allocation Need Available
A B C A B C A B C
P0 0 1 0 7 4 3 2 3 0
P1 3 0 2 0 2 0
P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
32/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Detecção de Deadlocks
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
33/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Instância única de cada tipo de recurso
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
34/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Grafo de Alocação de Recursos e Grafo de Espera
Grafo de Alocação de Recursos
Grafo correspondente de espera
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
35/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Várias instâncias de um tipo de recurso
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
36/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Algoritmo de Detecção
Se não existir tal i, vá para o passo 4
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
37/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Algoritmo de Detecção (cont.)
O algoritmo requer uma ordem de operações O(m x n2) para detectar se o sistema está em estado de deadlock
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
38/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Exemplo de algoritmo de detecção
Allocation Request Available
A B C A B C A B C
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
39/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Exemplo (cont.)
Request
A B C
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
40/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Uso do Algoritmo de Detecção
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
41/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Recuperação do Deadlock: Término de Processo
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
42/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Recuperação do Deadlock: Preempção de Recursos
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
43/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Bibliografia
Capítulo 2.
Capítulo 8.
(Capítulo 7 na 9a edição em Português).
44/44
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais