Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func worker(part string) {
|
||||
log.Println(part, "worker begins part")
|
||||
time.Sleep(time.Duration(rand.Int63n(1e6)))
|
||||
log.Println(part, "worker completes part")
|
||||
wg.Done()
|
||||
}
|
||||
|
||||
var (
|
||||
partList = []string{"A", "B", "C", "D"}
|
||||
nAssemblies = 3
|
||||
wg sync.WaitGroup
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
for c := 1; c <= nAssemblies; c++ {
|
||||
log.Println("begin assembly cycle", c)
|
||||
wg.Add(len(partList))
|
||||
for _, part := range partList {
|
||||
go worker(part)
|
||||
}
|
||||
wg.Wait()
|
||||
log.Println("assemble. cycle", c, "complete")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func worker(part string, completed chan string) {
|
||||
log.Println(part, "worker begins part")
|
||||
time.Sleep(time.Duration(rand.Int63n(1e6)))
|
||||
p := strings.ToLower(part)
|
||||
log.Println(part, "worker completed", p)
|
||||
completed <- p
|
||||
}
|
||||
|
||||
var (
|
||||
partList = []string{"A", "B", "C", "D"}
|
||||
nAssemblies = 3
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
completed := make([]chan string, len(partList))
|
||||
for i := range completed {
|
||||
completed[i] = make(chan string)
|
||||
}
|
||||
for c := 1; c <= nAssemblies; c++ {
|
||||
log.Println("begin assembly cycle", c)
|
||||
for i, part := range partList {
|
||||
go worker(part, completed[i])
|
||||
}
|
||||
a := ""
|
||||
for _, c := range completed {
|
||||
a += <-c
|
||||
}
|
||||
log.Println(a, "assembled. cycle", c, "complete")
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func worker(part string, completed chan string) {
|
||||
log.Println(part, "worker running")
|
||||
for {
|
||||
select {
|
||||
case <-start:
|
||||
log.Println(part, "worker begins part")
|
||||
time.Sleep(time.Duration(rand.Int63n(1e6)))
|
||||
p := strings.ToLower(part)
|
||||
log.Println(part, "worker completed", p)
|
||||
completed <- p
|
||||
<-reset
|
||||
wg.Done()
|
||||
case <-done:
|
||||
log.Println(part, "worker stopped")
|
||||
wg.Done()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
partList = []string{"A", "B", "C", "D"}
|
||||
nAssemblies = 3
|
||||
start = make(chan int)
|
||||
done = make(chan int)
|
||||
reset chan int
|
||||
wg sync.WaitGroup
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
completed := make([]chan string, len(partList))
|
||||
for i, part := range partList {
|
||||
completed[i] = make(chan string)
|
||||
go worker(part, completed[i])
|
||||
}
|
||||
for c := 1; c <= nAssemblies; c++ {
|
||||
log.Println("begin assembly cycle", c)
|
||||
reset = make(chan int)
|
||||
close(start)
|
||||
a := ""
|
||||
for _, c := range completed {
|
||||
a += <-c
|
||||
}
|
||||
log.Println(a, "assembled. cycle", c, "complete")
|
||||
wg.Add(len(partList))
|
||||
start = make(chan int)
|
||||
close(reset)
|
||||
wg.Wait()
|
||||
}
|
||||
wg.Add(len(partList))
|
||||
close(done)
|
||||
wg.Wait()
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const nMech = 5
|
||||
const detailsPerMech = 4
|
||||
|
||||
var l = log.New(os.Stdout, "", 0)
|
||||
|
||||
func main() {
|
||||
assemble := make(chan int)
|
||||
var complete sync.WaitGroup
|
||||
|
||||
go solicit(assemble, &complete, nMech*detailsPerMech)
|
||||
|
||||
for i := 1; i <= nMech; i++ {
|
||||
complete.Add(detailsPerMech)
|
||||
for j := 0; j < detailsPerMech; j++ {
|
||||
assemble <- 0
|
||||
}
|
||||
// Go checkpoint feature
|
||||
complete.Wait()
|
||||
// checkpoint reached
|
||||
l.Println("mechanism", i, "completed")
|
||||
}
|
||||
}
|
||||
|
||||
func solicit(a chan int, c *sync.WaitGroup, nDetails int) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
var id int // worker id, for output
|
||||
for nDetails > 0 {
|
||||
// some random time to find a worker
|
||||
time.Sleep(time.Duration(5e8 + rand.Int63n(5e8)))
|
||||
id++
|
||||
// contract to assemble a certain number of details
|
||||
contract := rand.Intn(5) + 1
|
||||
if contract > nDetails {
|
||||
contract = nDetails
|
||||
}
|
||||
dword := "details"
|
||||
if contract == 1 {
|
||||
dword = "detail"
|
||||
}
|
||||
l.Println("worker", id, "contracted to assemble", contract, dword)
|
||||
go worker(a, c, contract, id)
|
||||
nDetails -= contract
|
||||
}
|
||||
}
|
||||
|
||||
func worker(a chan int, c *sync.WaitGroup, contract, id int) {
|
||||
// some random time it takes for this worker to assemble a detail
|
||||
assemblyTime := time.Duration(5e8 + rand.Int63n(5e8))
|
||||
l.Println("worker", id, "enters shop")
|
||||
for i := 0; i < contract; i++ {
|
||||
<-a
|
||||
l.Println("worker", id, "assembling")
|
||||
time.Sleep(assemblyTime)
|
||||
l.Println("worker", id, "completed detail")
|
||||
c.Done()
|
||||
}
|
||||
l.Println("worker", id, "leaves shop")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue