2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"hash/fnv"
"log"
"math/rand"
"os"
@ -11,17 +12,19 @@ import (
// It is not otherwise fixed in the program.
var ph = []string{"Aristotle", "Kant", "Spinoza", "Marx", "Russell"}
const nBites = 3 // number of times each philosopher eats
const hunger = 3 // number of times each philosopher eats
const think = time.Second / 100 // mean think time
const eat = time.Second / 100 // mean eat time
var fmt = log.New(os.Stdout, "", 0) // for thread-safe output
var done = make(chan bool)
// This solution uses channels to implement synchronization.
// Sent over channels are "forks."
type fork byte
// A fork object in the program obviously models a physical fork in the
// philosopher simulation. In the more general context of resource
// sharing, the object would represent exclusive control of a resource.
// A fork object in the program models a physical fork in the simulation.
// A separate channel represents each fork place. Two philosophers
// have access to each fork. The channels are buffered with capacity = 1,
// representing a place for a single fork.
@ -31,17 +34,25 @@ type fork byte
func philosopher(phName string,
dominantHand, otherHand chan fork, done chan bool) {
fmt.Println(phName, "seated")
rg := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < nBites; i++ {
// each philosopher goroutine has a random number generator,
// seeded with a hash of the philosopher's name.
h := fnv.New64a()
h.Write([]byte(phName))
rg := rand.New(rand.NewSource(int64(h.Sum64())))
// utility function to sleep for a randomized nominal time
rSleep := func(t time.Duration) {
time.Sleep(t/2 + time.Duration(rg.Int63n(int64(t))))
}
for h := hunger; h > 0; h-- {
fmt.Println(phName, "hungry")
<-dominantHand // pick up forks
<-otherHand
fmt.Println(phName, "eating")
time.Sleep(time.Duration(rg.Int63n(1e8)))
rSleep(eat)
dominantHand <- 'f' // put down forks
otherHand <- 'f'
fmt.Println(phName, "thinking")
time.Sleep(time.Duration(rg.Int63n(1e8)))
rSleep(think)
}
fmt.Println(phName, "satisfied")
done <- true
@ -52,7 +63,6 @@ func main() {
fmt.Println("table empty")
// Create fork channels and start philosopher goroutines,
// supplying each goroutine with the appropriate channels
done := make(chan bool)
place0 := make(chan fork, 1)
place0 <- 'f' // byte in channel represents a fork on the table.
placeLeft := place0
@ -67,7 +77,7 @@ func main() {
// This makes precedence acyclic, preventing deadlock.
go philosopher(ph[0], place0, placeLeft, done)
// they are all now busy eating
for _ = range ph {
for range ph {
<-done // wait for philosphers to finish
}
fmt.Println("table empty")

View file

@ -0,0 +1,59 @@
package main
import (
"hash/fnv"
"log"
"math/rand"
"os"
"sync"
"time"
)
var ph = []string{"Aristotle", "Kant", "Spinoza", "Marx", "Russell"}
const hunger = 3
const think = time.Second / 100
const eat = time.Second / 100
var fmt = log.New(os.Stdout, "", 0)
var dining sync.WaitGroup
func philosopher(phName string, dominantHand, otherHand *sync.Mutex) {
fmt.Println(phName, "seated")
h := fnv.New64a()
h.Write([]byte(phName))
rg := rand.New(rand.NewSource(int64(h.Sum64())))
rSleep := func(t time.Duration) {
time.Sleep(t/2 + time.Duration(rg.Int63n(int64(t))))
}
for h := hunger; h > 0; h-- {
fmt.Println(phName, "hungry")
dominantHand.Lock() // pick up forks
otherHand.Lock()
fmt.Println(phName, "eating")
rSleep(eat)
dominantHand.Unlock() // put down forks
otherHand.Unlock()
fmt.Println(phName, "thinking")
rSleep(think)
}
fmt.Println(phName, "satisfied")
dining.Done()
fmt.Println(phName, "left the table")
}
func main() {
fmt.Println("table empty")
dining.Add(5)
fork0 := &sync.Mutex{}
forkLeft := fork0
for i := 1; i < len(ph); i++ {
forkRight := &sync.Mutex{}
go philosopher(ph[i], forkLeft, forkRight)
forkLeft = forkRight
}
go philosopher(ph[0], fork0, forkLeft)
dining.Wait() // wait for philosphers to finish
fmt.Println("table empty")
}