-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdining-philoshpher.go
58 lines (45 loc) · 1.05 KB
/
dining-philoshpher.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
package main
import (
"fmt"
"math"
"sync"
)
type Chopstick struct{ sync.Mutex }
type Philospher struct {
Id int
LeftChop *Chopstick
RightChop *Chopstick
}
func (p Philospher) eat(host chan int, wg *sync.WaitGroup) {
fmt.Printf("Allowed\n")
host <- 1
p.LeftChop.Lock()
p.RightChop.Lock()
fmt.Printf("%d Philospher started eating\n", p.Id)
p.RightChop.Unlock()
p.LeftChop.Unlock()
fmt.Printf("%d Philospher completed eating\n", p.Id)
fmt.Printf("Removed\n")
<-host
wg.Done()
}
func main() {
host := make(chan int, 2)
var wg sync.WaitGroup
fmt.Printf("Starting Dinning Philospher problem\n")
chopsticks := make([]*Chopstick, 5)
philospher := make([]Philospher, 5)
for i := range chopsticks {
chopsticks[i] = new(Chopstick)
}
for i := range philospher {
philospher[i] = Philospher{i, chopsticks[int(math.Min(float64(i), float64((i+1)%5)))], chopsticks[int(math.Max(float64((i+1)%5), float64(i)))]}
}
for i := 0; i < 5; i++ {
for times := 0; times < 3; times++ {
wg.Add(1)
go philospher[i].eat(host, &wg)
}
}
wg.Wait()
}