This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.02-Algoritmo_Backery
68 lines (56 loc) · 2.44 KB
/
07.02-Algoritmo_Backery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Algoritmo Backery
==================
* Características
- Não depende do Fetch-and-Add
- Reduz a chande de overflows
- Tenta reproduzir outro esquema junto do atendimento do mundo real
- Os processos vão tentar entrar na seção crítica. Ao fazer
isso, ao invés de olhar o visor, eles olham as senhas dos
outros processos e selecionam uma senha maior que a de todos.
* CSEntrada e CSSaída
- CSEntrada
| <senha[i] = max(senha[1:n]) + 1;>
| for (j = 1 to st i != j) {
| <await(senha[j] == 0 or senha[i] < senha[j]); >
- Apesar de parecer estranho, não precisamos voltar ao início
do vetor. Com certeza um processo que já foi comparado,
quando atendido, terá uma senha maior que o processo que
está olhando.
- Só paramos e esperamos quando encontramos alguém cuja senha
seja menor que a nossa. Só seremos atendidos depois deles.
- CSSaída
| senha[i] = 0;
* 1ª implementação (com await's)
| int senha[1:n] = ([n], 0);
| process CS[i=1 to n] {
| while(true) {
| <senha[i] = max(senha[1:n]) + 1;>
| /* Não existe instrução de máquina para executar 'max' */
| for [j=1 to n st j != i)
| <await (senha[j] == 0 or senha[i] < senha[j]);>
| /* Não segue a PNMUV */
| /* região crítica */
| senha[i] = 0;
| /* região não-crítica */
| }
| }
- Novamente, temos '<await (B);>'. Precisamos de alguma maneira de
implementar as regiões críticas.
- O 2º await mais interno tem um problema extra: sua condição
interna não respeita a PNMUV.
* 2ª implementação (incorreta)
- Vamos começar pensando para 2 processos
- CS1
| senha1 = senha2 + 1;
| while (senha2 != 0 and senha1 > senha2) skip;
- CS2
| senha2 = senha1 + 1;
| while (senha1 != 0 and senha2 > senha1) skip;
- Problemas
- Ambos os processos podem ficar esperando indefinidamente,
em um livelock.
- Inicialmente, 'senha1' e 'senha2' são 0. Se ambos os valores
forem carregados na 1ª atribuição, teremos que ambos 'senha1'
e 'senha2' serão 1. Eles passam pela primeira condição, e a
segunda é sempre falsa. Espera indefinida.
- Precisamos de uma maneira de corrigir esse problema.