From 6bef24c294c50f51527b1506b5c75d4f46f17761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Wed, 17 Feb 2021 10:40:04 +0100 Subject: [PATCH] add stuck examples --- examples/stuck-deadlock/go.mod | 3 ++ examples/stuck-deadlock/main.go | 48 +++++++++++++++++++++++ examples/stuck-producer-worker/go.mod | 3 ++ examples/stuck-producer-worker/main.go | 54 ++++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 examples/stuck-deadlock/go.mod create mode 100644 examples/stuck-deadlock/main.go create mode 100644 examples/stuck-producer-worker/go.mod create mode 100644 examples/stuck-producer-worker/main.go diff --git a/examples/stuck-deadlock/go.mod b/examples/stuck-deadlock/go.mod new file mode 100644 index 0000000..36dcc3b --- /dev/null +++ b/examples/stuck-deadlock/go.mod @@ -0,0 +1,3 @@ +module github.com/felixge/go-profiler-notes/examples/stuck-program + +go 1.15 diff --git a/examples/stuck-deadlock/main.go b/examples/stuck-deadlock/main.go new file mode 100644 index 0000000..3a22c79 --- /dev/null +++ b/examples/stuck-deadlock/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "os" + "os/signal" + "sync" +) + +func main() { + if err := run(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} +func run() error { + var ( + a = &sync.Mutex{} + b = &sync.Mutex{} + ) + go bob(a, b) + go alice(a, b) + + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + return nil +} + +func bob(a, b *sync.Mutex) { + for { + fmt.Println("bob is okay") + a.Lock() + b.Lock() + a.Unlock() + b.Unlock() + } +} + +func alice(a, b *sync.Mutex) { + for { + fmt.Println("alice is okay") + b.Lock() + a.Lock() + b.Unlock() + a.Unlock() + } +} diff --git a/examples/stuck-producer-worker/go.mod b/examples/stuck-producer-worker/go.mod new file mode 100644 index 0000000..36dcc3b --- /dev/null +++ b/examples/stuck-producer-worker/go.mod @@ -0,0 +1,3 @@ +module github.com/felixge/go-profiler-notes/examples/stuck-program + +go 1.15 diff --git a/examples/stuck-producer-worker/main.go b/examples/stuck-producer-worker/main.go new file mode 100644 index 0000000..fe07396 --- /dev/null +++ b/examples/stuck-producer-worker/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "math/rand" + "os" + "os/signal" + "time" +) + +func main() { + if err := run(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func run() error { + rand.Seed(time.Now().UnixNano()) + + workCh := make(chan int) + go worker(workCh) + go producer(workCh) + + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + return nil +} + +func producer(workCh chan<- int) { + for msg := 0; ; msg++ { + workCh <- msg + fmt.Printf("producer sent: %d\n", msg) + if rand.Int63n(10) == 0 { + takeNap() + } + } +} + +func worker(workCh <-chan int) { + for { + msg := <-workCh + fmt.Printf("worker received: %d\n", msg) + if rand.Int63n(10) == 0 { + takeNap() + } + } +} + +func takeNap() { + var forever chan struct{} + <-forever +}