MC504 Sistemas Operacionais
Prof. Dr. Eng. Isaías Bittencourt Felzmann
isaias@ic.unicamp.br
Campinas, 2s/2026
Sincronização de Processos
1/41
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/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Objetivos
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
3/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Contexto
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
4/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Condição de Corrida
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
5/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Problema da Seção Crítica
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
6/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Seção Crítica
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
7/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Problema da Seção Crítica (Cont.)
Requisitos para uma solução do problema da seção crítica
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
8/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Solução baseada em interrupções
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
9/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
A Solução de Peterson
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
10/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Algoritmo para o Processo Pi
while (true) {
flag[i] = true;
turn = j;
while (flag[j] && turn == j)
;
/* critical section */
flag[i] = false;
/* remainder section */
}
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
11/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Correção da Solução de Peterson
1. A exclusão mútua é preservada
Pi entra na seção crítica somente se:
flag[j] = false ou turn = i
2. O requisito de progresso é atendido
3. O requisito de espera limitada é atendido
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
12/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
A Solução de Peterson e a Arquitetura Moderna
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
13/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Barreira de Memória (Fence)
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
14/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Instruções de Barreira de Memória
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
15/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Hardware de Sincronização
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
16/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Instruções de Hardware
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
17/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
A instrução test_and_set
boolean test_and_set(boolean *target)
{
boolean rv = *target;
*target = true;
return rv;
}
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
18/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Solução Usando test_and_set()
do {� while (test_and_set(&lock))
; /* do nothing */�
/* critical section */�
lock = false;
/* remainder section */
} while (true);
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
19/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
A instrução compare_and_swap
int compare_and_swap(int *value, int expected, int new_value)
{
int temp = *value;
if (*value == expected)
*value = new_value;
return temp;
}
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
20/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Solução usando compare_and_swap
while (true) {� while (compare_and_swap(&lock, 0, 1) != 0)
; /* do nothing */�
/* critical section */�
lock = 0;�
/* remainder section */
}
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
21/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Espera limitada com compare_and_swap
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
22/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Variáveis atômicas
increment(&sequence);
garante que sequence seja incrementada sem interrupção:��
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
23/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Variáveis atômicas
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
24/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Locks Mutex
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
25/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Solução do problema da seção crítica usando locks mutex
while (true) {
acquire lock
critical section
release lock
remainder section
}
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
26/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Semáforo
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
signal(S) {
S++;
}
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
27/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Semáforo (continuação)
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
28/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Exemplo de Uso de Semáforo
wait(mutex);
CS
signal(mutex);
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
29/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Implementação com Semáforo
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
30/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Problemas com Semáforos
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
31/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Problema dos Filósofos do Jantar
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
32/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Algoritmo do Problema dos Filósofos do Jantar
while (true) {
wait(chopstick[i]);
wait(chopstick[(i + 1) % 5]);
/* eat for a while */
signal(chopstick[i]);
signal(chopstick[(i + 1) % 5]);
/* think for a while */
}
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
33/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Locks Mutex POSIX
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
34/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Semáforos POSIX sem nome
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
35/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Semáforos POSIX nomeados
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
36/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Liveness (Progresso)
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
37/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Liveness (Progresso)
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
38/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Deadlock!
39/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Liveness (Progresso)
Silberschatz, Galvin e Gagne ©2018
Operating System Concepts – 10th Edition
40/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais
Bibliografia
Capítulo 2.
Capítulos 6 e 7.
(Capítulo 5 na 9a edição em Português).
41/41
rev. 2s/2026
IC/UNICAMP – MC504 Sistemas Operacionais