-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
354 lines (276 loc) · 7.33 KB
/
main.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"fmt"
"math"
"math/rand"
"sync"
"time"
)
//Cores do Print
var colorReset = "\033[0m"
var colorRed = "\033[31m"
var colorGreen = "\033[32m"
var colorYellow = "\033[33m"
var colorPurple = "\033[35m"
var colorCyan = "\033[36m"
//tipo de display
var display bool
//Filosofo
type Filosofo struct {
id int
estado int
//-2 - Terminou
//-1 - Morreu
//0 - Pensando
//1 - Com Fome
//2 - Comendo
filEsq *Filosofo
filDir *Filosofo
hashiEsq *Hashi
hashiDir *Hashi
}
//Print Filosofo
func (f *Filosofo) String() string {
res := "filosofo: " + fmt.Sprint(f.id) + "\n"
res += "estado: " + fmt.Sprint(f.estado) + "\n"
res += "filEsq: " + fmt.Sprint(f.filEsq.id) + "\n"
res += "fisDir: " + fmt.Sprint(f.filDir.id) + "\n"
res += "hashiEsq: " + fmt.Sprint(f.hashiEsq.id) + "\n"
res += "hashiDir: " + fmt.Sprint(f.hashiDir.id) + "\n"
return res
}
//Hashi
type Hashi struct {
id int
disponivel bool
reservado int
// -1 esquerdo reservou
// 0 ninguem reservou
// 1 direito reservou
}
func (f *Filosofo) comeca(wg *sync.WaitGroup) {
defer wg.Done()
//Iteracoes do filosofo
for i := 0; i < 5; i++ {
time.Sleep(time.Duration(rand.Intn(3)+1) * time.Second)
//Estado aleatorio
state := rand.Intn(2)
stateAnt := f.estado
f.estado = state
switch state {
case 0:
if !display && stateAnt != 0 {
fmt.Printf("Filosofo %d: %sPensando%s\n", f.id, string(colorGreen), string(colorReset))
}
time.Sleep(time.Duration(rand.Intn(6)+4) * time.Second)
case 1:
if !display {
fmt.Printf("Filosofo %d: %sCom fome%s\n", f.id, string(colorYellow), string(colorReset))
}
if f.hashiEsq.disponivel && f.hashiDir.disponivel && f.hashiEsq.reservado != -1 && f.hashiDir.reservado != 1 {
//Comendo
f.hashiEsq.disponivel = false
f.hashiDir.disponivel = false
if !display {
fmt.Printf("Filosofo %d: %sComendo com hashis %d e %d%s\n", f.id, string(colorCyan), f.hashiEsq.id, f.hashiDir.id, string(colorReset))
}
f.estado = 2
time.Sleep(time.Duration(rand.Intn(8)+2) * time.Second)
if !display {
fmt.Printf("Filosofo %d: %sTerminou de comer. Liberando hashis %d e %d%s\n", f.id, string(colorPurple), f.hashiEsq.id, f.hashiDir.id, string(colorReset))
}
f.estado = 0
f.hashiEsq.disponivel = true
f.hashiDir.disponivel = true
} else {
//Esperando
if !display {
fmt.Printf("Filosofo %d: %sEsperando para comer%s\n", f.id, string(colorYellow), string(colorReset))
}
for i := 0; !f.hashiEsq.disponivel || !f.hashiDir.disponivel || f.hashiEsq.reservado == -1 || f.hashiDir.reservado == 1; i++ {
if !f.hashiEsq.disponivel && !f.hashiDir.disponivel {
f.hashiEsq.reservado = 1
f.hashiDir.reservado = -1
} else if !f.hashiEsq.disponivel {
f.hashiEsq.reservado = 1
} else if !f.hashiDir.disponivel {
f.hashiDir.reservado = -1
}
//Morreu
if i > int(math.Pow(10, 9)) {
f.estado = -1
//Desreservar hashis
if f.hashiEsq.reservado == 1 {
f.hashiEsq.reservado = 0
}
if f.hashiDir.reservado == -1 {
f.hashiDir.reservado = 0
}
if !display {
fmt.Printf("%sFilosofo %d: Morreu de fome%s\n", string(colorRed), f.id, string(colorReset))
}
return
}
}
//Comer depois de esperar
f.hashiEsq.disponivel = false
f.hashiDir.disponivel = false
f.hashiEsq.reservado = 0
f.hashiDir.reservado = 0
if !display {
fmt.Printf("Filosofo %d: %sComendo com hashis %d e %d%s\n", f.id, string(colorCyan), f.hashiEsq.id, f.hashiDir.id, string(colorReset))
}
f.estado = 2
time.Sleep(time.Duration(rand.Intn(8)+2) * time.Second)
if !display {
fmt.Printf("Filosofo %d: %sTerminou de comer. Liberando hashis %d e %d%s\n", f.id, string(colorPurple), f.hashiEsq.id, f.hashiDir.id, string(colorReset))
}
f.estado = 0
f.hashiEsq.disponivel = true
f.hashiDir.disponivel = true
}
default:
fmt.Println("!Erro de estado!")
}
}
f.estado = -2
}
//Inicializar Mesa de Filosofos
func initFilosofos(total int) []*Filosofo {
filosofos := make([]*Filosofo, total, total)
auxHashi := &Hashi{1, true, 0}
filosofos[0] = &Filosofo{
id: 1,
estado: 0,
filEsq: nil,
filDir: nil,
hashiEsq: auxHashi,
hashiDir: nil,
}
for i := 1; i < total; i++ {
auxHashi = &Hashi{i + 1, true, 0}
filosofos[i-1].hashiDir = auxHashi
filosofos[i] = &Filosofo{
id: i + 1,
estado: 0,
filEsq: filosofos[i-1],
filDir: nil,
hashiEsq: auxHashi,
hashiDir: nil,
}
filosofos[i-1].filDir = filosofos[i]
}
filosofos[total-1].filDir = filosofos[0]
filosofos[total-1].hashiDir = filosofos[0].hashiEsq
filosofos[0].filEsq = filosofos[total-1]
return filosofos
}
func checarFilosofos(filosofos []*Filosofo) bool {
for _, f := range filosofos {
if f.estado != -2 && f.estado != -1 {
return true
}
}
return false
}
func main() {
//Entradas
//Total de filosofos
var total int
fmt.Print("\033[H\033[2J")
fmt.Print("Numero de filosofos: ")
_, err := fmt.Scanln(&total)
fmt.Print("\033[H\033[2J")
for err != nil || total < 0 {
fmt.Printf("%s!Invalido!%s\n", colorRed, colorReset)
fmt.Print("Numero de filosofos: ")
_, err = fmt.Scanln(&total)
fmt.Print("\033[H\033[2J")
}
//Tipo de display
var tipo int
fmt.Print("1- Dinamico\n2- Impresso\n\nTipo de display: ")
_, err = fmt.Scanln(&tipo)
for err != nil || tipo != 1 && tipo != 2 {
fmt.Printf("%s!Invalido!%s\n", colorRed, colorReset)
fmt.Print("1- Dinamico\n2- Impresso\n\nTipo de display: ")
_, err = fmt.Scanln(&tipo)
fmt.Print("\033[H\033[2J")
}
display = tipo == 1
//Seed random
rand.Seed(time.Now().UTC().UnixNano())
//Criar Filosofos e Hashis
filosofos := initFilosofos(total)
//debug
//for _, f := range filosofos {
// fmt.Println(f.String())
//}
division := "-------------------------"
if !display {
fmt.Print("\033[H\033[2J")
fmt.Println(division, "Inicio", division)
}
//Grupo thread
wg := new(sync.WaitGroup)
wg.Add(total)
//Inicializar Filosofos
for _, f := range filosofos {
if !display {
fmt.Printf("Filosofo %d: %sPensando%s\n", f.id, colorGreen, colorReset)
}
go f.comeca(wg)
}
//Display
if display {
for {
res := "\033[H\033[2J"
time.Sleep(500 * time.Millisecond)
for _, f := range filosofos {
//Hashi
if !f.hashiEsq.disponivel {
res += colorRed
} else if f.hashiEsq.reservado != 0 {
res += colorYellow
} else {
res += colorGreen
}
res += "Hashi " + fmt.Sprint(f.hashiEsq.id) + "\n"
//Filosofo
if f.estado == 0 {
res += colorGreen
} else if f.estado == 1 {
res += colorCyan
} else if f.estado == 2 {
res += colorYellow
} else if f.estado == 3 {
res += colorPurple
} else if f.estado == -1 {
res += colorRed
} else {
res += colorReset
}
res += "Filosofo " + fmt.Sprint(f.id) + "\n"
}
//Hashi repetido
if !filosofos[0].hashiEsq.disponivel {
res += colorRed
} else if filosofos[0].hashiEsq.reservado != 0 {
res += colorYellow
} else {
res += colorGreen
}
res += "Hashi " + fmt.Sprint(filosofos[0].hashiEsq.id) + colorReset + "\n"
//Print de tudo
fmt.Println(res)
if !checarFilosofos(filosofos) {
break
}
}
}
//Esperar threads terminarem
wg.Wait()
if !display {
fmt.Println(division, "Fim", division)
}
}