Understanding How Concurrency Work in OS
Learning Conurrency in Python, Java and Golang
G7
genchi.lu@cymetrics.io
Concurrency in OS
Concurrency in OS - Scheduler
Concurrency in OS - Thread and Process
code data file
register stack
code data file
register register
register
stack
stack
stack
thread
thread
single thread process multi-thread process
Concurrency in CPython
Concurrency in CPython - How
● multi thread (OS native thread)
● multi process
● async (it’s more about async io)
Concurrency in CPython - Thread (1)
Concurrency in CPython - Thread (2)
number of python’s threads is equal
to number of OS’s thread
Concurrency in CPython - Thread And GIL (1)
Constrain of Python thread: GIL
There always only one thread is running per python process.
Concurrency in CPython - Thread And GIL (2)
Total CPU usage is always about 100%, no matter how many cores your machine
is.
Concurrency in CPython - Process (1)
All CPU is full of usage
Concurrency in CPython - Process (2)
Empirically gauging the difference in
overhead can be difficult, but in
general it is much more time
consuming to create and manage
processes than threads.
Concurrency in CPython - Process (3)
Concurrency in Java
Concurrency in Java - How
multi thread (OS native thread)
Concurrency in Java - Thread(1)
Concurrency in Java - Thread (2)
number of Java’s threads is great
then number of OS’s thread
(Some threads for the purpose of
maintaining JVM, like GC)
Concurrency in Java - memory cost of thread (1)
Concurrency in Java - memory cost of thread (1)
● java -cp java-1.0-SNAPSHOT.jar ThreadNum
● java -cp java-1.0-SNAPSHOT.jar -Xss200k ThreadNum
● java -cp java-1.0-SNAPSHOT.jar -Xss1g ThreadNum
Concurrency in Java - Overhead of Thread (1)
● Create Thread
● Destroy Thread
● Context Switch
Concurrency in Java - Overhead of Thread (2)
Concurrency in Java - Overhead of Thread (3)
Concurrency in Java - Profile Overhead(1)
number of threads = 10
Concurrency in Java - Profile Overhead (2)
number of threads = 5000
Concurrency in Golang
Concurrency in Golang - Goroutine on Thread (1)
Concurrency in Golang - Goroutine on Thread (2)
● Pick Strategy
○ Pick up one goroutine from thread’s local queue
○ Pick up one goroutine from global queue
○ Network Poller
○ Pick up one goroutine from other thread’s local queue (work stealing)
● Every thread would execute one goroutine about 10ms, then switch.
● Golang uses GOMAXPROCS parameter to number of threads to use.
○ default: number of cores.
Concurrency in Golang - Goroutine on Thread (3)
Concurrency in Golang - Goroutine on Thread (4)
number of goroutines is much less
then number of OS’s thread
Concurrency in Golang - Goroutine on Thread (5)
Concurrency in Golang - Blocking System Call (1)
Concurrency in Golang - Blocking System Call (2)
Concurrency in Golang - Blocking System Call (3)
Concurrency in Golang - Blocking System Call (4)
Concurrency in Golang - Blocking System Call (5)
Concurrency in Golang - Blocking System Call (6)
the number of OS’s threads is
growing to hundreds.
Recap
Recap
● How concurrency work in OS level
● How CPython implement concurrency on top of OS
○ multi thread
○ multi process
● How Java implement concurrency on top of OS
○ Thread
● The challenge of thread model
● How golang implement concurrency on top of OS
○ goroutine
Final Though
Final Though - Python not suitable in High Concurrency?
● Instagram use Python to build their product
● “It’s hard to do” does not mean “It CAN NOT”
Final Though - Evolution of Java’s Concurrency
● Java had use green thread before Java 1.2
● As green threads have some limitations compared to native threads,
subsequent Java versions dropped them in favor of native threads
● Quasar Project
● A library that provides high-performance lightweight threads.
● Loom Project
● Introduce fibers as lightweight, efficient threads managed by the Java
Virtual Machine
Final Though - Goroutine is not Silver Bullet
● Goroutine make is easy to handle concurrency
● Does not mean that you can use goroutine in whatever scenario without cost
○ ex. high concurrency with blocking system call
QA
All Sample Code
Reference
● Understanding the Python GIL
● Go scheduler: Implementing language with lightweight concurrency
● The Go netpoller
● The Go scheduler
● Going inside Java’s Project Loom and virtual threads
● Green threads
● Quasar
● Loom Project
● Adopting Python Asyncio in Large Scale Project
● Scaling Instagram Infrastructure
Thank You
About Cymetrics
● Tech Blog

Understanding how concurrency work in os

  • 1.
    Understanding How ConcurrencyWork in OS Learning Conurrency in Python, Java and Golang G7 genchi.lu@cymetrics.io
  • 2.
  • 3.
    Concurrency in OS- Scheduler
  • 4.
    Concurrency in OS- Thread and Process code data file register stack code data file register register register stack stack stack thread thread single thread process multi-thread process
  • 5.
  • 6.
    Concurrency in CPython- How ● multi thread (OS native thread) ● multi process ● async (it’s more about async io)
  • 7.
  • 8.
    Concurrency in CPython- Thread (2) number of python’s threads is equal to number of OS’s thread
  • 9.
    Concurrency in CPython- Thread And GIL (1) Constrain of Python thread: GIL There always only one thread is running per python process.
  • 10.
    Concurrency in CPython- Thread And GIL (2) Total CPU usage is always about 100%, no matter how many cores your machine is.
  • 11.
  • 12.
    All CPU isfull of usage Concurrency in CPython - Process (2)
  • 13.
    Empirically gauging thedifference in overhead can be difficult, but in general it is much more time consuming to create and manage processes than threads. Concurrency in CPython - Process (3)
  • 14.
  • 15.
    Concurrency in Java- How multi thread (OS native thread)
  • 16.
  • 17.
    Concurrency in Java- Thread (2) number of Java’s threads is great then number of OS’s thread (Some threads for the purpose of maintaining JVM, like GC)
  • 18.
    Concurrency in Java- memory cost of thread (1)
  • 19.
    Concurrency in Java- memory cost of thread (1) ● java -cp java-1.0-SNAPSHOT.jar ThreadNum ● java -cp java-1.0-SNAPSHOT.jar -Xss200k ThreadNum ● java -cp java-1.0-SNAPSHOT.jar -Xss1g ThreadNum
  • 20.
    Concurrency in Java- Overhead of Thread (1) ● Create Thread ● Destroy Thread ● Context Switch
  • 21.
    Concurrency in Java- Overhead of Thread (2)
  • 22.
    Concurrency in Java- Overhead of Thread (3)
  • 23.
    Concurrency in Java- Profile Overhead(1) number of threads = 10
  • 24.
    Concurrency in Java- Profile Overhead (2) number of threads = 5000
  • 25.
  • 26.
    Concurrency in Golang- Goroutine on Thread (1)
  • 27.
    Concurrency in Golang- Goroutine on Thread (2) ● Pick Strategy ○ Pick up one goroutine from thread’s local queue ○ Pick up one goroutine from global queue ○ Network Poller ○ Pick up one goroutine from other thread’s local queue (work stealing) ● Every thread would execute one goroutine about 10ms, then switch. ● Golang uses GOMAXPROCS parameter to number of threads to use. ○ default: number of cores.
  • 28.
    Concurrency in Golang- Goroutine on Thread (3)
  • 29.
    Concurrency in Golang- Goroutine on Thread (4) number of goroutines is much less then number of OS’s thread
  • 30.
    Concurrency in Golang- Goroutine on Thread (5)
  • 31.
    Concurrency in Golang- Blocking System Call (1)
  • 32.
    Concurrency in Golang- Blocking System Call (2)
  • 33.
    Concurrency in Golang- Blocking System Call (3)
  • 34.
    Concurrency in Golang- Blocking System Call (4)
  • 35.
    Concurrency in Golang- Blocking System Call (5)
  • 36.
    Concurrency in Golang- Blocking System Call (6) the number of OS’s threads is growing to hundreds.
  • 37.
  • 38.
    Recap ● How concurrencywork in OS level ● How CPython implement concurrency on top of OS ○ multi thread ○ multi process ● How Java implement concurrency on top of OS ○ Thread ● The challenge of thread model ● How golang implement concurrency on top of OS ○ goroutine
  • 39.
  • 40.
    Final Though -Python not suitable in High Concurrency? ● Instagram use Python to build their product ● “It’s hard to do” does not mean “It CAN NOT”
  • 41.
    Final Though -Evolution of Java’s Concurrency ● Java had use green thread before Java 1.2 ● As green threads have some limitations compared to native threads, subsequent Java versions dropped them in favor of native threads ● Quasar Project ● A library that provides high-performance lightweight threads. ● Loom Project ● Introduce fibers as lightweight, efficient threads managed by the Java Virtual Machine
  • 42.
    Final Though -Goroutine is not Silver Bullet ● Goroutine make is easy to handle concurrency ● Does not mean that you can use goroutine in whatever scenario without cost ○ ex. high concurrency with blocking system call
  • 43.
  • 44.
  • 45.
    Reference ● Understanding thePython GIL ● Go scheduler: Implementing language with lightweight concurrency ● The Go netpoller ● The Go scheduler ● Going inside Java’s Project Loom and virtual threads ● Green threads ● Quasar ● Loom Project ● Adopting Python Asyncio in Large Scale Project ● Scaling Instagram Infrastructure
  • 46.
  • 47.