-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (42 loc) · 1001 Bytes
/
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
package main
import (
"fmt"
)
const (
// Number seats at the table
seats = 5
// Number of portions on each plate
portions = 3
)
func main() {
// Create a waiter
waiter := &Waiter{
// Number of philosophers this waiter can serve at once
serves: make(chan bool),
// Number of requests a waiter can take
requests: make(chan bool),
// Flag for service finished
done: make(chan bool),
}
// Create seats * chopsticks
chopSticks := make([]*ChopStick, seats)
for i := 0; i < seats; i++ {
chopSticks[i] = new(ChopStick)
}
// Create seats * philosophers
philosophers := make([]*Philosopher, seats)
for i := 0; i < seats; i++ {
philosophers[i] = &Philosopher{
id: i + 1,
left: chopSticks[i],
right: chopSticks[(i+1)%seats],
waiter: waiter,
food: Food{portions},
done: make(chan bool),
}
}
fmt.Println("Table Service Has Started!")
// Start serving the philosophers
waiter.serve(philosophers)
fmt.Println("Table Service Has Finished!")
}