Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
84
Task/Dining-philosophers/Go/dining-philosophers-1.go
Normal file
84
Task/Dining-philosophers/Go/dining-philosophers-1.go
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"hash/fnv"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Number of philosophers is simply the length of this list.
|
||||
// It is not otherwise fixed in the program.
|
||||
var ph = []string{"Aristotle", "Kant", "Spinoza", "Marx", "Russell"}
|
||||
|
||||
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 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.
|
||||
|
||||
// Goroutine for philosopher actions. An instance is run for each
|
||||
// philosopher. Instances run concurrently.
|
||||
func philosopher(phName string,
|
||||
dominantHand, otherHand chan fork, done chan bool) {
|
||||
fmt.Println(phName, "seated")
|
||||
// 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")
|
||||
rSleep(eat)
|
||||
dominantHand <- 'f' // put down forks
|
||||
otherHand <- 'f'
|
||||
fmt.Println(phName, "thinking")
|
||||
rSleep(think)
|
||||
}
|
||||
fmt.Println(phName, "satisfied")
|
||||
done <- true
|
||||
fmt.Println(phName, "left the table")
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("table empty")
|
||||
// Create fork channels and start philosopher goroutines,
|
||||
// supplying each goroutine with the appropriate channels
|
||||
place0 := make(chan fork, 1)
|
||||
place0 <- 'f' // byte in channel represents a fork on the table.
|
||||
placeLeft := place0
|
||||
for i := 1; i < len(ph); i++ {
|
||||
placeRight := make(chan fork, 1)
|
||||
placeRight <- 'f'
|
||||
go philosopher(ph[i], placeLeft, placeRight, done)
|
||||
placeLeft = placeRight
|
||||
}
|
||||
// Make one philosopher left handed by reversing fork place
|
||||
// supplied to philosopher's dominant hand.
|
||||
// This makes precedence acyclic, preventing deadlock.
|
||||
go philosopher(ph[0], place0, placeLeft, done)
|
||||
// they are all now busy eating
|
||||
for range ph {
|
||||
<-done // wait for philosphers to finish
|
||||
}
|
||||
fmt.Println("table empty")
|
||||
}
|
||||
59
Task/Dining-philosophers/Go/dining-philosophers-2.go
Normal file
59
Task/Dining-philosophers/Go/dining-philosophers-2.go
Normal 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")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue