Skip to content

Commit

Permalink
allow all philoshopers to eat
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangbits committed Jul 13, 2020
1 parent 8942b99 commit 787cc5b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 10 deletions.
73 changes: 73 additions & 0 deletions c3-m4/PeerReview/philo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"fmt"
"strconv"
"sync"
"time"
)

type ChopS struct {
sync.Mutex
}

type Philosopher struct {
leftCS, rightCS *ChopS
name string
nbrTimeEat int
}

func main() {

var wg sync.WaitGroup
wg.Add(15)
c := make(chan *Philosopher,2)

Csticks := make([] *ChopS, 5)
for i := 0; i<5; i++ {
Csticks[i] = new(ChopS)
}

philos := make([] *Philosopher, 5)
for i := 0; i<5; i++ {
philos[i] = &Philosopher{Csticks[(i+1)%5],Csticks[i], strconv.Itoa(i+1),0}
}


go host(c)
for i := 0; i<5; i++ {
go philos[i].eat(c,&wg)
}

wg.Wait()

}

func (p Philosopher) eat(channel chan *Philosopher, wait *sync.WaitGroup) {
for {
channel <- &p
if (p.nbrTimeEat<3) {
p.leftCS.Lock()
p.rightCS.Lock()

fmt.Printf("starting to eat %v\n", p.name)
p.nbrTimeEat = p.nbrTimeEat+1
//Eating take time
time.Sleep(300 * time.Millisecond)
fmt.Printf("finishing eating %v\n", p.name)

p.rightCS.Unlock()
p.leftCS.Unlock()
wait.Done()
}
}
}

func host(c chan *Philosopher) {
for {
if (len(c)==2) {
<- c
<- c
}
}
}
21 changes: 11 additions & 10 deletions c3-m4/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Philo struct {
leftCS, rightCS *ChopS
}

func (p Philo) eat(semanphore chan int) {
func (p Philo) eat(semanphore chan int, wg *sync.WaitGroup) {
// 2.Each philosopher should eat only 3 times (not in an infinite loop as we did in lecture)
for i := 0; i < 3; i++ {
p.leftCS.Lock()
Expand All @@ -30,8 +30,9 @@ func (p Philo) eat(semanphore chan int) {
p.rightCS.Unlock()
// 8.When a philosopher finishes eating
fmt.Println("finishing eating ", p.name)
<-semanphore // removes an int from semanphore, allowing another to proceed
}
<-semanphore // removes an int from semanphore, allowing another to proceed
wg.Done()
}

// MaxConcurrency Maximum number of gorountine run concurrency
Expand All @@ -54,15 +55,15 @@ func main() {
philos[i] = &Philo{i + 1, CSticks[(i+1)%5], CSticks[i]}
}
semanphore := make(chan int, MaxConcurrency)
for {

for i := 0; i < 5; i++ {
// 5.The host allows no more than 2 philosophers to eat concurrently.
semanphore <- 1 // will block if there is MaxConccurrency in semanphore
// 4.In order to eat, a philosopher must get permission from a host which executes in its own goroutine.
go philos[i].eat(semanphore)
}
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
// 5.The host allows no more than 2 philosophers to eat concurrently.
semanphore <- 1 // will block if there is MaxConccurrency in semanphore
// 4.In order to eat, a philosopher must get permission from a host which executes in its own goroutine.
go philos[i].eat(semanphore, &wg)
}
wg.Wait()
}

// RandBool This function returns a random boolean value based on the current time
Expand Down

0 comments on commit 787cc5b

Please sign in to comment.