help the dining philosophers

Hello,

I am working on the dining philosophers program

There should be 5 philosophers sharing chopsticks, with a stick between each pair of adjacent philosophers.
Each philosopher must eat only 3 times
Philosophers pick up chopsticks in any order, not with the lowest number first
To eat, a philosopher must obtain permission from a host that runs in his own routine.
The host does not allow more than 2 philosophers to eat simultaneously.
Each philosopher is numbered from 1 to 5.
When a philosopher begins to eat (after having obtained the necessary blocks) prints "beginning to eat " on a line, where is the philosopher’s number.
When a philosopher finishes eating (before releasing his blocks), he prints "finishing eating " on a line, where is the philosopher’s number.

type ChopS struct{ sync.Mutex }
type Philo struct {
leftCS, rightCS * ChopS
}
//Eat
func (p Philo) eat() {
for {
p.leftCS.Lock()
p.rightCS.Lock()
fmt.Println(“eating”)
p.rightCS.Unlock()
p.leftCS.Unlock()
}
}
//main
CSticks := make([]*ChopS, 5)
for i := 0; i < 5; i++ {
CSticks[i] = new(ChopS)
}
philos := make([]*Philo, 5)
for i := 0; i < 5; i++ {
philos[i] = &Philo{Csticks[i], Csticks[(i+1)%5]}
}
CSticks := make([]*ChopS, 5)
for i := 0; i < 5; i++ {
CSticks[i] = new(ChopS)
}
philos := make([]*Philo, 5)
for i := 0; i < 5; i++ {
philos[i] = &Philo{Csticks[i], Csticks[(i+1)%5]}
}
// cena main
for i := 0; i < 5; i++ {
go philos[i].eat()
}
//comida
p.leftCS.Lock()
p.rightCS.Lock()
fmt.Println(“eating”)
p.rightCS.Unlock()
p.leftCS.Unlock()

1 Like

Welcome to Golang Bridge :rocket::fireworks:

You’re facing exactly the same problem as your classmate did back some time ago: LINK.

Feel free to track his/her findings.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.