SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

PDF
My talk at LVEE 2016
Alex Chistyakov
 
PDF
My talk on Piter Py 2016
Alex Chistyakov
 
PDF
Go Programming Language by Google
Uttam Gandhi
 
PDF
welcome to gopherlabs - why go (golang)?
sangam biradar
 
PDF
Go at Skroutz
AgisAnastasopoulos
 
PDF
Decision making - for loop , nested loop ,if-else statements , switch in goph...
sangam biradar
 
PDF
Dependency management in golang
Ramit Surana
 
PPTX
Open-Source Analytics Stack on MongoDB, with Schema, Pierre-Alain Jachiet and...
Pôle Systematic Paris-Region
 
PDF
Why you should care about Go (Golang)
Aaron Schlesinger
 
PPTX
Geb+spock: let your functional tests live long and prosper
Esther Lozano
 
ODP
eXo EC - Groovy Programming Language
Hoat Le
 
PDF
Spring-batch Groovy y Gradle
Antonio Mas
 
PDF
Groovy and noteworthy
Izzet Mustafaiev
 
PDF
Mphasis Digital - Use Go (gloang) for system programming, distributed systems...
Aniruddha Chakrabarti
 
PDF
Buildr - build like you code
Izzet Mustafaiev
 
PDF
Golang
Felipe Mamud
 
PDF
How to write a well-behaved Python command line application
gjcross
 
PDF
Groovy android
Mario García
 
PDF
Dsl로 만나는 groovy
Seeyoung Chang
 
My talk at LVEE 2016
Alex Chistyakov
 
My talk on Piter Py 2016
Alex Chistyakov
 
Go Programming Language by Google
Uttam Gandhi
 
welcome to gopherlabs - why go (golang)?
sangam biradar
 
Go at Skroutz
AgisAnastasopoulos
 
Decision making - for loop , nested loop ,if-else statements , switch in goph...
sangam biradar
 
Dependency management in golang
Ramit Surana
 
Open-Source Analytics Stack on MongoDB, with Schema, Pierre-Alain Jachiet and...
Pôle Systematic Paris-Region
 
Why you should care about Go (Golang)
Aaron Schlesinger
 
Geb+spock: let your functional tests live long and prosper
Esther Lozano
 
eXo EC - Groovy Programming Language
Hoat Le
 
Spring-batch Groovy y Gradle
Antonio Mas
 
Groovy and noteworthy
Izzet Mustafaiev
 
Mphasis Digital - Use Go (gloang) for system programming, distributed systems...
Aniruddha Chakrabarti
 
Buildr - build like you code
Izzet Mustafaiev
 
Golang
Felipe Mamud
 
How to write a well-behaved Python command line application
gjcross
 
Groovy android
Mario García
 
Dsl로 만나는 groovy
Seeyoung Chang
 

Similar to Understanding how concurrency work in os (20)

PDF
concurrency
Jonathan Wagoner
 
PDF
Concurrency in Python4k
Rodolfo Carvalho
 
PDF
Introduction to Concurrency
Sri Prasanna
 
PDF
Python VS GO
Ofir Nir
 
PDF
Multiprocessing with python
Patrick Vergain
 
PDF
threads (1).pdfmjlkjfwjgliwiufuaiusyroayr
abhinandpk2405
 
PDF
Concurrency
Sri Prasanna
 
PDF
Concurrent programming1
Nick Brandaleone
 
PPT
Threads in java, Multitasking and Multithreading
ssusere538f7
 
PPTX
Python UNIT-IV Multi Threading B.Tech CSE
SrinuTelugu
 
PDF
Concurrency in Golang
Oliver N
 
PDF
Introduction to Ruby threads
Luong Vo
 
PPTX
Golang 101 (Concurrency vs Parallelism)
Pramesti Hatta K.
 
KEY
Do more than one thing at the same time, the Python way
Jaime Buelta
 
PDF
Threads lecture slides for operating systems
amirtarek401
 
PDF
Python, do you even async?
Saúl Ibarra Corretgé
 
PDF
Concurrency and Python - PyCon MY 2015
Boey Pak Cheong
 
PDF
A first look into the Project Loom in Java
Lukas Steinbrecher
 
PPTX
Networking threads
Nilesh Pawar
 
PPTX
Go: What's Different ?
Tarun Vashisth
 
concurrency
Jonathan Wagoner
 
Concurrency in Python4k
Rodolfo Carvalho
 
Introduction to Concurrency
Sri Prasanna
 
Python VS GO
Ofir Nir
 
Multiprocessing with python
Patrick Vergain
 
threads (1).pdfmjlkjfwjgliwiufuaiusyroayr
abhinandpk2405
 
Concurrency
Sri Prasanna
 
Concurrent programming1
Nick Brandaleone
 
Threads in java, Multitasking and Multithreading
ssusere538f7
 
Python UNIT-IV Multi Threading B.Tech CSE
SrinuTelugu
 
Concurrency in Golang
Oliver N
 
Introduction to Ruby threads
Luong Vo
 
Golang 101 (Concurrency vs Parallelism)
Pramesti Hatta K.
 
Do more than one thing at the same time, the Python way
Jaime Buelta
 
Threads lecture slides for operating systems
amirtarek401
 
Python, do you even async?
Saúl Ibarra Corretgé
 
Concurrency and Python - PyCon MY 2015
Boey Pak Cheong
 
A first look into the Project Loom in Java
Lukas Steinbrecher
 
Networking threads
Nilesh Pawar
 
Go: What's Different ?
Tarun Vashisth
 
Ad

Recently uploaded (20)

PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PPTX
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
Digital water marking system project report
Kamal Acharya
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Digital water marking system project report
Kamal Acharya
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Ad

Understanding how concurrency work in os