From 42c2105b16f67fe3e113f0e71ce8ee7208790dc0 Mon Sep 17 00:00:00 2001 From: dwrz Date: Fri, 5 Jun 2020 16:13:03 +0000 Subject: [PATCH] Update concurrency slides --- concurrency/slides.org | 177 ++++++++++++++++++++++------------------- 1 file changed, 95 insertions(+), 82 deletions(-) diff --git a/concurrency/slides.org b/concurrency/slides.org index 0cf993c..4f2fbe5 100644 --- a/concurrency/slides.org +++ b/concurrency/slides.org @@ -61,7 +61,7 @@ A second is a duration, not an instant. Two things can happen in the same second and not be simultaneous. -In computer science, this nuance matters. +With computers, this nuance matters. * 9. Time @@ -98,10 +98,12 @@ Source: [[https://www.prowesscorp.com/computer-latency-at-a-human-scale/]] * 12. Cooking Recipes - Step-by-step -- Concurrent - - Pre-heat oven - - Boil water - - Timers +- Concurrent: + - Handle other tasks while: + - Preheating oven + - Boiling water + - Waiting for timers + - Switching back to a task when it is ready for more work. * 13. Multitasking @@ -117,24 +119,28 @@ Computers can "simultaneously" work on: Combination of: - Operating System *Scheduler* - Hardware (/interrupts/) + - Keyboard + - Filesystem + - Network Card + - Timer -* 17. Cooperative Scheduling +* 16. Cooperative Scheduling [[file:images/cooperative-scheduling.jpg]] -* 18. Preemptive Scheduling +* 17. Preemptive Scheduling [[file:images/preemptive-scheduling.jpg]] -* 19. Preemptive Scheduling +* 18. Preemptive Scheduling [[file:images/preemptive-scheduling-tree.jpg]] -* 20. Multi Core +* 19. Multi Core [[file:images/preemptive-scheduling-multi-core.jpg]] -* 21. Concurrency versus Parallelism +* 20. Concurrency versus Parallelism - Parallelism :: performing computations at the same time. - Multiple cores. - - Multiples computers. + - Multiple computers. - Kind of like having multiple cooks in a kitchen. - Can do different things at the same time. - Can do one job faster (peeling a lot of potatoes). @@ -142,13 +148,14 @@ Combination of: - Concurrency :: performing computations over inter-leaving, non-sequential time periods. - Single or multi-core. -* 22. Processes and Threads +* 21. Processes and Threads A little more nuance. - Different /recipes/ - Different /parts/ of a recipe - Process :: a program in execution. + - Unique resources (memory, code, files, etc.). - Start off with one thread. - Can /fork/. - Communicate via inter-process communication (IPC). @@ -157,17 +164,17 @@ A little more nuance. - Share resources ∴ less resource intensive. - Can communicate via shared resources. -* 23. Processes and Threads +* 22. Processes and Threads [[file:images/process-vs-thread.jpg]] Source: https://stackoverflow.com/questions/16354460/forking-vs-threading/16354658 -* 24. htop +* 23. htop Press ~H~ to show threads. -* 25. Languages and Runtimes +* 24. Languages and Runtimes - ~C~, ~C++~, ~Rust~ allow for fine-grained control of processes and threads. #+begin_src C :output raw @@ -180,7 +187,7 @@ int main() { } #+end_src -* 26. JavaScript +* 25. JavaScript - Single-threaded (for application code). - Runtimes implement concurrency via an event loop. @@ -188,42 +195,7 @@ int main() { Source: [[https://medium.com/preezma/node-js-event-loop-architecture-go-deeper-node-core-c96b4cec7aa4]] -* 27. Go -Runtime multi-threading, multiplexes goroutines onto threads. - -[[file:images/go-scheduler.png]] - -Source: https://freethreads.net/2019/01/23/go-runtime-scheduler-design-internals/ - -* 28. Quick Review - -- Sequential versus Concurrent Steps - - Compare with recipes. -- Concurrency versus Parallelism - - Parallelism: multiple cooks, simultaneously /doing/. - - Concurrency: one or more cooks, simultaneously /dealing/ with many tasks. -- Operating System Scheduler -- Processes and Threads -- Languages and Runtimes - -* 29. Part 2: Using Concurrency - -Typical use cases: -- Network I/O - - Requests between clients and servers. - - Requests to a database. - - Requests between microservices. - - Requests to external APIs. - -- User interfaces - - Capturing user input - - Rendering output - -- Similar, but independent computations. - -- Solving problems that are best expressed with concurrency. - -* 30. JavaScript +* 26. Aside: Event Loop What is the output of the following? Why? #+begin_src javascript @@ -238,14 +210,49 @@ setTimeout(() => console.log('2'), 0); console.log('end'); #+end_src -* 31. Go Examples +* 27. Go +*Runtime scheduler* manages multiple threads. +[[file:images/go-scheduler.png]] + +Source: https://freethreads.net/2019/01/23/go-runtime-scheduler-design-internals/ + +* 28. Quick Review + +- Sequential versus Concurrent Steps + - Compare with recipes. +- Concurrency versus Parallelism + - Parallelism: multiple cooks, simultaneously /doing/. + - Concurrency: one or more cooks, /dealing/ with many tasks. +- Operating System Scheduler +- Processes and Threads +- Languages and Runtimes + +* 29. Part 2: Using Concurrency + +Typical use cases: +- Network I/O between: + - Clients and servers. + - Services and database(s). + - Microservices. + - Service and external APIs. + +- User interfaces + - Capturing user input + - Rendering output + +- Similar, but independent computations. + +- Solving problems that are best expressed with concurrency. + +* 30. Go Examples 1. Shared Memory with Mutex (Mutual Exclusion) - Pitfall: synchronization, debugging. -2. Message Passing (Channels and Goroutines) - - Pitfall: resources usage, complexity. -* 32. Mutex Example +2. Message Passing (Channels and Goroutines) + - Pitfall: resource usage, complexity. + +* 31. Mutex Example Concurrent updates to a bank account balance. Code: @@ -253,31 +260,37 @@ Code: Run: [[file:cmd/mutex/run.sh]] - Without mutex. - - With mutex. -* 33. Data Races +* 32. Data Races [[file:images/data-race.jpg]] -* 34. Mutex +* 33. Mutex /Mutual Exclusion/ - Purpose: protect a critical section of code. - Effect: serialization. -* 35. Race Detector +Run: [[file:cmd/mutex/run.sh]] + - With mutex. + +* 34. Race Detector + +How can we be sure? + +Race detector can give us /some/ confidence (but no guarantees). - Without mutex. - With mutex. -* 36. Tradeoffs: Validating Correctness, Testing, Debugging +* 35. Tradeoffs: Validating Correctness, Testing, Debugging - Non-deterministic behavior - Works fine on one run, fatal bug in another. - Up to how the scheduler(s) order things at execution time. - Concealed bugs -* 37. Message Passing +* 36. Message Passing Proverbs: #+begin_quote @@ -286,7 +299,7 @@ Don't communicate by sharing memory, share memory by communicating. Channels orchestrate, mutexes serialize. #+end_quote -* 38. Requirements +* 37. Program Requirements - Read CSV of zip codes - 50 in our example @@ -298,27 +311,27 @@ Channels orchestrate, mutexes serialize. - Display running list of top ten temperatures. -* 39. Concurrency Hallmarks +* 38. Concurrency Hallmarks - Repeatedly processing similar data (lines of CSV) - Network I/O - UI I/O -* 40. Message Passing Example +* 39. Message Passing Example Run: [[file:cmd/csp/run.sh]] -* 41. Code Walkthrough +* 40. Code Walkthrough [[file:cmd/csp/main.go]] -* 42. Channels and Goroutines +* 41. Channels and Goroutines [[file:images/message-passing-example-diagram.jpg]] -* 43. Channels and Goroutines +* 42. Channels and Goroutines [[file:images/go-scheduler.jpg]] -* 44. Scaling +* 43. Scaling What if: - We wanted to process the entire list of 42,000 zip codes? @@ -326,7 +339,7 @@ What if: Could we just get rid of the limiter? -* 45. Resources +* 44. Resources Probably not, because: @@ -339,12 +352,12 @@ Probably not, because: - Better data structures and algorithms could help here. - Goroutines are cheap, but not free. -* 46. Possible Solutions +* 45. Possible Solutions - Use a semaphore to limit concurrency. - Buffers -* 47. Designing and Managing Concurrency +* 46. Designing and Managing Concurrency - Size of input matters. - Resources @@ -353,7 +366,7 @@ Probably not, because: - Backpressure - Complexity -* 48. Quick Review +* 47. Quick Review Techniques for implementing and managing concurrency: - Event loop @@ -367,7 +380,7 @@ Concurrency Pitfalls: - Complexity - Need to think differently about concurrent code. -* 49. Part 3: Infrastructure Concurrency +* 48. Part 3: Infrastructure Concurrency Concurrent applications running concurrently on distributed computers. @@ -376,20 +389,20 @@ Concurrent applications running concurrently on distributed computers. - Databases - Host and Network Infrastructure -* 50. Example +* 49. Example [[file:images/infrastructure-data-race.jpg]] -* 51. Review +* 50. Review - What concurrency is - Sequential versus concurrent - - Comparison with recipes + - Comparison with recipes and cooks - Compared to parallelism - How it is implemented - Operating System Scheduler and Interrupts - Languages and Runtimes -* 52. Review +* 51. Review - When, why, and how to use it - I/O + UI @@ -398,16 +411,16 @@ Concurrent applications running concurrently on distributed computers. - Pitfalls - Synchronization - - Resources Usage + - Resource Usage - Debugging - Complexity -* 53. Review +* 52. Review - Infrastructure - Data Race -* 54. Conclusion +* 53. Conclusion - Goals: - Give you a rough sense of what concurrency is. @@ -421,7 +434,7 @@ Concurrent applications running concurrently on distributed computers. - Tradeoffs and economics (resources, costs, developer time). - Learning is continuous... -* 55. Further Reading +* 54. Further Reading - [[https://slikts.github.io/concurrency-glossary/][Concurrency Glossary]] - [[https://blog.golang.org/waza-talk][Concurrency is not parallelism]]