SlideShare a Scribd company logo
1 of 19
Linux multiplexing
Mark Veltzer
mailto: mark@veltzer.net
What is multiplexing?
● Multiplexing is doing multiple IO operations in
just one thread.
● It is also used in the more general meaning of
waiting for more than one thing to happen.
● In our case it means waiting for more than one
thing to happen at the OS level (usually IO to
complete).
● This could be IO, signal, event from another
thread, timer and more.
Multiplexing and multi-threading
● Some see multiplexing as an alternative to multi
threading.
● If one thread can do IO on many file descriptors and
if IO is slow then one thread may handle all the IO
needs of the application.
● This view has a lot of truth to it and practice has
shown that you can implement a very efficient web
server using a single thread.
● Note that this has nothing to do with content
generation or business logic. They may need threads
because they are CPU consuming.
Multiplexing and multi-threading
(cont)
● Multiplexing can also be used in concert with multi-
threading.
● Either one thread will do multiplexing and will use
a thread pool to do business logic.
● Or you could have more than one multiplexer (with
it's own thread pool, or not...)
● Because of the rising speed of network devices
you may need more than one multiplexer.
● Obviously then you need to divide connections/file
descriptors between your multiplexing threads.
What about signal handling?
● Signal handling could be seen as a sort of multiplexing.
● It enables you to do more than one thing in one thread:
both wait for a signal and do synchronous IO at the
same time.
● But signal handling is asynchronous which causes
problems for many programmers.
● While multiplexing is synchronous (albeit not easy to
implement correctly).
● And signal handling was never scalable to a large
number of concurrent tasks.
What about asynchronous IO?
● Asynchronous IO does allow you to do
something like multiplexing: do IO and not wait
for it to complete.
● But how would you know that the IO has
completed?
● You either get a signal with signal handling
problems.
● Or wait. But that is exactly what multiplexing
does.
The reactor pattern
● The reactor pattern in ACE and other frameworks is a pattern
built on top of the OS specific multiplexing API.
● You write multiple objects with methods to handle various types
of events.
● The framework then uses a single thread to wait for any of these
events using the OS API.
● When the event happens the thread wakes up and calls the
appropriate method on the appropriate object.
● And you get a nice C++ OO API so that you can easily write the
event handlers independent of each other.
File descriptors in UNIX
● File descriptors in UNIX were flexible to begin with.
● They could stand for:
● Regular file / folders
● Device driver
● Network socket
● Listening socket
● Anonymous or named pipes
● UNIX domain socket
● Virtual files, like /proc, which look like regular files
● So multiplexing on many file descriptors is already
powerful.
File descriptors in Linux
● In order to make multiplexing more flexible and
effective the concept of file descriptor was
enhanced in Linux to include things that were
never in the UNIX definition.
● In Linux a file descriptor can also be:
● Timer (timerfd)
● Signal file descriptor for synchronous signal
delivery. (signalfd)
● Event from another thread or process (eventfd)
timerfd
● API: timerfd_create(2), timerfd_settime(2),
timerfd_gettime(2)
● You create (open?!?) a timerfd by calling
timerfd_create.
● You can say what type of clock (hardware, software,
etc) your timer will be based on.
● You get a file descriptor.
● Now you can read(2) it and you block until the timer
expires.
● Or you can multiplex on it.
signalfd
● API: signalfd(2)
● You call signalfd(2) and create (open?!?) a file
descriptor.
● You can specify what signals you want this file
descriptor to catch and various flags.
● You can now read(2) this file descriptor and go
to sleep and wait for any of the prescribed
signals.
● Or you can multiplex on it.
eventfd
● API: eventfd(2), eventfd_read(2), eventfd_write(2)
● Create (open?) an eventfd using eventfd(2)
● Now write or read from it.
● Writes and reads only work in 64 bit chunks.
● You can now read(2) this file descriptor and go to
sleep to wait for this event. You will wake up once
another thread does a write(2) to this file descriptor.
● Or you can multiplex on it.
eventfd (cont)
● More efficient than a pipe (no circular buffer,
messages are constant size).
● Simpler than a pipe (one fd and not two, no
signals involved).
● Kernel uses this to signal user space (async io).
● You can use it for inter-thread or inter-process
communication.
● For instance using fork.
Linux multiplexing API
● Is comprised of three system calls (or system
call groups): select, poll and epoll.
● They all work on file descriptors.
● Since file descriptor in Linux can stand for all
the previously mentioned types multiplexing is
very flexible.
● You pack file descriptors into a group and then
you wait on the group.
Why 3 different APIs?
● Because they evolved through time.
● First was select, then poll and then epoll.
● Differences are: size of group of fds, efficient
handling of large groups of fds, timer support,
signal masking and more.
● Epoll is the most advanced API so we will
concentrate on that.
epoll
● API: epoll(2), epoll_create(2), epoll_create1(2),
epoll_ctl(2), epoll_pwait(2), epoll_wait(2)
● You create (open?) an epoll file descriptor using
epoll_create(2).
● Now you can add/remove file descriptors
to/from it using epoll_ctl.
● When adding a file descriptor you can say what
you are waiting on this file descriptor for (read,
write, error, etc).
epoll (cont)
● Now you can epoll_wait(2) until something
happens.
● When you wake up your array of epll_event
structs is filled with details about what
happened.
● You handle the events and go back to
epoll_wait(2).
epoll (cont)
● In epoll the list of file descriptors is held in
kernel space.
● This is seen as one of epolls main strengths
since passing a large file descriptor collection on
each call to select(2) or poll(2) was problematic.
● This is what makes epoll(2) scale to large
numbers of file descriptors (some report
hundreds of thousands).
● Some see this as the pinnacle of Linux APIs.
Multiplexing - conclusions
● Multiplexing in Linux is very powerful.
● You can do many many things in parallel in a single
thread/process.
● It scales well to large numbers of file descriptors.
● The API is not easy but that is the kernel design.
● The idea is that it will be wrapped by user level libraries,
maybe even OO.
● ACE needs to be compiled in a special way to support
epoll.

More Related Content

What's hot

How Functions Work
How Functions WorkHow Functions Work
How Functions WorkSaumil Shah
 
IPv4aaS tutorial and hands-on
IPv4aaS tutorial and hands-onIPv4aaS tutorial and hands-on
IPv4aaS tutorial and hands-onAPNIC
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017OpenEBS
 
Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545Kernel TLV
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqRuben Tan
 
Asynchronous programming intro
Asynchronous programming introAsynchronous programming intro
Asynchronous programming introcc liu
 
Course lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented ProgrammingCourse lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented ProgrammingJonathan Salwan
 
Introduction to netlink in linux kernel (english)
Introduction to netlink in linux kernel (english)Introduction to netlink in linux kernel (english)
Introduction to netlink in linux kernel (english)Sneeker Yeh
 
0.5mln packets per second with Erlang
0.5mln packets per second with Erlang0.5mln packets per second with Erlang
0.5mln packets per second with ErlangMaxim Kharchenko
 
Dive into ROP - a quick introduction to Return Oriented Programming
Dive into ROP - a quick introduction to Return Oriented ProgrammingDive into ROP - a quick introduction to Return Oriented Programming
Dive into ROP - a quick introduction to Return Oriented ProgrammingSaumil Shah
 
Stackless Python In Eve
Stackless Python In EveStackless Python In Eve
Stackless Python In Eveguest91855c
 
Systemtap
SystemtapSystemtap
SystemtapFeng Yu
 
Python twisted
Python twistedPython twisted
Python twistedMahendra M
 
Introduction to Erlang/(Elixir) at a Webilea Hands-On Session
Introduction to Erlang/(Elixir) at a Webilea Hands-On SessionIntroduction to Erlang/(Elixir) at a Webilea Hands-On Session
Introduction to Erlang/(Elixir) at a Webilea Hands-On SessionAndré Graf
 
Startup guide for kvm on cent os 6
Startup guide for kvm on cent os 6Startup guide for kvm on cent os 6
Startup guide for kvm on cent os 6Carlos Eduardo
 
RAFT Consensus Algorithm
RAFT Consensus AlgorithmRAFT Consensus Algorithm
RAFT Consensus Algorithmsangyun han
 

What's hot (20)

Distributed Elixir
Distributed ElixirDistributed Elixir
Distributed Elixir
 
How Functions Work
How Functions WorkHow Functions Work
How Functions Work
 
IPv4aaS tutorial and hands-on
IPv4aaS tutorial and hands-onIPv4aaS tutorial and hands-on
IPv4aaS tutorial and hands-on
 
Return Oriented Programming (ROP) Based Exploits - Part I
Return Oriented Programming  (ROP) Based Exploits  - Part IReturn Oriented Programming  (ROP) Based Exploits  - Part I
Return Oriented Programming (ROP) Based Exploits - Part I
 
LuaJIT
LuaJITLuaJIT
LuaJIT
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
 
Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromq
 
Asynchronous programming intro
Asynchronous programming introAsynchronous programming intro
Asynchronous programming intro
 
Course lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented ProgrammingCourse lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented Programming
 
Introduction to netlink in linux kernel (english)
Introduction to netlink in linux kernel (english)Introduction to netlink in linux kernel (english)
Introduction to netlink in linux kernel (english)
 
0.5mln packets per second with Erlang
0.5mln packets per second with Erlang0.5mln packets per second with Erlang
0.5mln packets per second with Erlang
 
Dive into ROP - a quick introduction to Return Oriented Programming
Dive into ROP - a quick introduction to Return Oriented ProgrammingDive into ROP - a quick introduction to Return Oriented Programming
Dive into ROP - a quick introduction to Return Oriented Programming
 
Stackless Python In Eve
Stackless Python In EveStackless Python In Eve
Stackless Python In Eve
 
Advance ROP Attacks
Advance ROP AttacksAdvance ROP Attacks
Advance ROP Attacks
 
Systemtap
SystemtapSystemtap
Systemtap
 
Python twisted
Python twistedPython twisted
Python twisted
 
Introduction to Erlang/(Elixir) at a Webilea Hands-On Session
Introduction to Erlang/(Elixir) at a Webilea Hands-On SessionIntroduction to Erlang/(Elixir) at a Webilea Hands-On Session
Introduction to Erlang/(Elixir) at a Webilea Hands-On Session
 
Startup guide for kvm on cent os 6
Startup guide for kvm on cent os 6Startup guide for kvm on cent os 6
Startup guide for kvm on cent os 6
 
RAFT Consensus Algorithm
RAFT Consensus AlgorithmRAFT Consensus Algorithm
RAFT Consensus Algorithm
 

Viewers also liked

Mba ii rm unit-3.3 sampling methods a
Mba ii rm unit-3.3 sampling methods aMba ii rm unit-3.3 sampling methods a
Mba ii rm unit-3.3 sampling methods aRai University
 
Sampling Design and Sampling Distribution
Sampling Design and Sampling DistributionSampling Design and Sampling Distribution
Sampling Design and Sampling DistributionVikas Sonwane
 
Toxic Release And Dispersion Models
Toxic Release And Dispersion ModelsToxic Release And Dispersion Models
Toxic Release And Dispersion Modelsleksonobangun
 
The Physical Layer
The Physical LayerThe Physical Layer
The Physical Layeradil raja
 
Best Practices for Salesforce Data Access
Best Practices for Salesforce Data AccessBest Practices for Salesforce Data Access
Best Practices for Salesforce Data AccessSalesforce Developers
 
Intorduction to cellular communication
Intorduction to cellular communicationIntorduction to cellular communication
Intorduction to cellular communicationZaahir Salam
 
Comparison between ipv4 and ipv6
Comparison between ipv4 and ipv6Comparison between ipv4 and ipv6
Comparison between ipv4 and ipv6Dharmesh Patel
 
GSM, Cell Planning & Frequency Reuse
GSM, Cell Planning & Frequency ReuseGSM, Cell Planning & Frequency Reuse
GSM, Cell Planning & Frequency Reusesanjida2222
 
IPv4 to IPv6
IPv4 to IPv6IPv4 to IPv6
IPv4 to IPv6mithilak
 
Wireless communication and cellular concept
Wireless communication and cellular conceptWireless communication and cellular concept
Wireless communication and cellular conceptsaam123
 

Viewers also liked (15)

08 multiplexing
08 multiplexing08 multiplexing
08 multiplexing
 
Sampling (Research)
Sampling (Research)Sampling (Research)
Sampling (Research)
 
Mba ii rm unit-3.3 sampling methods a
Mba ii rm unit-3.3 sampling methods aMba ii rm unit-3.3 sampling methods a
Mba ii rm unit-3.3 sampling methods a
 
Sampling Design and Sampling Distribution
Sampling Design and Sampling DistributionSampling Design and Sampling Distribution
Sampling Design and Sampling Distribution
 
Toxic Release And Dispersion Models
Toxic Release And Dispersion ModelsToxic Release And Dispersion Models
Toxic Release And Dispersion Models
 
Sample
SampleSample
Sample
 
The Physical Layer
The Physical LayerThe Physical Layer
The Physical Layer
 
Physical Layer
Physical LayerPhysical Layer
Physical Layer
 
Best Practices for Salesforce Data Access
Best Practices for Salesforce Data AccessBest Practices for Salesforce Data Access
Best Practices for Salesforce Data Access
 
Intorduction to cellular communication
Intorduction to cellular communicationIntorduction to cellular communication
Intorduction to cellular communication
 
Comparison between ipv4 and ipv6
Comparison between ipv4 and ipv6Comparison between ipv4 and ipv6
Comparison between ipv4 and ipv6
 
Sdh total final
Sdh total finalSdh total final
Sdh total final
 
GSM, Cell Planning & Frequency Reuse
GSM, Cell Planning & Frequency ReuseGSM, Cell Planning & Frequency Reuse
GSM, Cell Planning & Frequency Reuse
 
IPv4 to IPv6
IPv4 to IPv6IPv4 to IPv6
IPv4 to IPv6
 
Wireless communication and cellular concept
Wireless communication and cellular conceptWireless communication and cellular concept
Wireless communication and cellular concept
 

Similar to Linux multiplexing

An introduction to erlang
An introduction to erlangAn introduction to erlang
An introduction to erlangMirko Bonadei
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!C4Media
 
Building scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftBuilding scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftTalentica Software
 
Linux Server Deep Dives (DrupalCon Amsterdam)
Linux Server Deep Dives (DrupalCon Amsterdam)Linux Server Deep Dives (DrupalCon Amsterdam)
Linux Server Deep Dives (DrupalCon Amsterdam)Amin Astaneh
 
Linux 开源操作系统发展新趋势
Linux 开源操作系统发展新趋势Linux 开源操作系统发展新趋势
Linux 开源操作系统发展新趋势Anthony Wong
 
Building scalable and language-independent Java services using Apache Thrift ...
Building scalable and language-independent Java services using Apache Thrift ...Building scalable and language-independent Java services using Apache Thrift ...
Building scalable and language-independent Java services using Apache Thrift ...IndicThreads
 
M|18 Architectural Overview: MariaDB MaxScale
M|18 Architectural Overview: MariaDB MaxScaleM|18 Architectural Overview: MariaDB MaxScale
M|18 Architectural Overview: MariaDB MaxScaleMariaDB plc
 
Monitoring.pptx
Monitoring.pptxMonitoring.pptx
Monitoring.pptxShadi Akil
 
[CB16] COFI break – Breaking exploits with Processor trace and Practical cont...
[CB16] COFI break – Breaking exploits with Processor trace and Practical cont...[CB16] COFI break – Breaking exploits with Processor trace and Practical cont...
[CB16] COFI break – Breaking exploits with Processor trace and Practical cont...CODE BLUE
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.jsSu Zin Kyaw
 
BUD17-104: Scripting Languages in IoT: Challenges and Approaches
BUD17-104: Scripting Languages in IoT: Challenges and ApproachesBUD17-104: Scripting Languages in IoT: Challenges and Approaches
BUD17-104: Scripting Languages in IoT: Challenges and ApproachesLinaro
 
A Dive Into ELF Binaries
A Dive Into ELF BinariesA Dive Into ELF Binaries
A Dive Into ELF BinariesBhashit Pandya
 
Centralized logging system using mongoDB
Centralized logging system using mongoDBCentralized logging system using mongoDB
Centralized logging system using mongoDBVivek Parihar
 
Analysis Result Manager (ARM)
Analysis Result Manager (ARM)Analysis Result Manager (ARM)
Analysis Result Manager (ARM)magland
 
TFLite NNAPI and GPU Delegates
TFLite NNAPI and GPU DelegatesTFLite NNAPI and GPU Delegates
TFLite NNAPI and GPU DelegatesKoan-Sin Tan
 

Similar to Linux multiplexing (20)

Multicore
MulticoreMulticore
Multicore
 
An introduction to erlang
An introduction to erlangAn introduction to erlang
An introduction to erlang
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
 
Building scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thriftBuilding scalable and language independent java services using apache thrift
Building scalable and language independent java services using apache thrift
 
Linux Server Deep Dives (DrupalCon Amsterdam)
Linux Server Deep Dives (DrupalCon Amsterdam)Linux Server Deep Dives (DrupalCon Amsterdam)
Linux Server Deep Dives (DrupalCon Amsterdam)
 
Linux 开源操作系统发展新趋势
Linux 开源操作系统发展新趋势Linux 开源操作系统发展新趋势
Linux 开源操作系统发展新趋势
 
Building scalable and language-independent Java services using Apache Thrift ...
Building scalable and language-independent Java services using Apache Thrift ...Building scalable and language-independent Java services using Apache Thrift ...
Building scalable and language-independent Java services using Apache Thrift ...
 
M|18 Architectural Overview: MariaDB MaxScale
M|18 Architectural Overview: MariaDB MaxScaleM|18 Architectural Overview: MariaDB MaxScale
M|18 Architectural Overview: MariaDB MaxScale
 
Monitoring.pptx
Monitoring.pptxMonitoring.pptx
Monitoring.pptx
 
[CB16] COFI break – Breaking exploits with Processor trace and Practical cont...
[CB16] COFI break – Breaking exploits with Processor trace and Practical cont...[CB16] COFI break – Breaking exploits with Processor trace and Practical cont...
[CB16] COFI break – Breaking exploits with Processor trace and Practical cont...
 
Multithreading by rj
Multithreading by rjMultithreading by rj
Multithreading by rj
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
Linux-Internals-and-Networking
Linux-Internals-and-NetworkingLinux-Internals-and-Networking
Linux-Internals-and-Networking
 
BUD17-104: Scripting Languages in IoT: Challenges and Approaches
BUD17-104: Scripting Languages in IoT: Challenges and ApproachesBUD17-104: Scripting Languages in IoT: Challenges and Approaches
BUD17-104: Scripting Languages in IoT: Challenges and Approaches
 
Monkey Server
Monkey ServerMonkey Server
Monkey Server
 
A Dive Into ELF Binaries
A Dive Into ELF BinariesA Dive Into ELF Binaries
A Dive Into ELF Binaries
 
Centralized logging system using mongoDB
Centralized logging system using mongoDBCentralized logging system using mongoDB
Centralized logging system using mongoDB
 
Analysis Result Manager (ARM)
Analysis Result Manager (ARM)Analysis Result Manager (ARM)
Analysis Result Manager (ARM)
 
TFLite NNAPI and GPU Delegates
TFLite NNAPI and GPU DelegatesTFLite NNAPI and GPU Delegates
TFLite NNAPI and GPU Delegates
 
Realtime
RealtimeRealtime
Realtime
 

More from Mark Veltzer (11)

Gcc
GccGcc
Gcc
 
Gcc opt
Gcc optGcc opt
Gcc opt
 
Linux io
Linux ioLinux io
Linux io
 
Linux logging
Linux loggingLinux logging
Linux logging
 
Linux monitoring
Linux monitoringLinux monitoring
Linux monitoring
 
Linux tmpfs
Linux tmpfsLinux tmpfs
Linux tmpfs
 
Multicore
MulticoreMulticore
Multicore
 
Pthreads linux
Pthreads linuxPthreads linux
Pthreads linux
 
Streams
StreamsStreams
Streams
 
Volatile
VolatileVolatile
Volatile
 
Effective cplusplus
Effective cplusplusEffective cplusplus
Effective cplusplus
 

Recently uploaded

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 

Recently uploaded (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Linux multiplexing

  • 2. What is multiplexing? ● Multiplexing is doing multiple IO operations in just one thread. ● It is also used in the more general meaning of waiting for more than one thing to happen. ● In our case it means waiting for more than one thing to happen at the OS level (usually IO to complete). ● This could be IO, signal, event from another thread, timer and more.
  • 3. Multiplexing and multi-threading ● Some see multiplexing as an alternative to multi threading. ● If one thread can do IO on many file descriptors and if IO is slow then one thread may handle all the IO needs of the application. ● This view has a lot of truth to it and practice has shown that you can implement a very efficient web server using a single thread. ● Note that this has nothing to do with content generation or business logic. They may need threads because they are CPU consuming.
  • 4. Multiplexing and multi-threading (cont) ● Multiplexing can also be used in concert with multi- threading. ● Either one thread will do multiplexing and will use a thread pool to do business logic. ● Or you could have more than one multiplexer (with it's own thread pool, or not...) ● Because of the rising speed of network devices you may need more than one multiplexer. ● Obviously then you need to divide connections/file descriptors between your multiplexing threads.
  • 5. What about signal handling? ● Signal handling could be seen as a sort of multiplexing. ● It enables you to do more than one thing in one thread: both wait for a signal and do synchronous IO at the same time. ● But signal handling is asynchronous which causes problems for many programmers. ● While multiplexing is synchronous (albeit not easy to implement correctly). ● And signal handling was never scalable to a large number of concurrent tasks.
  • 6. What about asynchronous IO? ● Asynchronous IO does allow you to do something like multiplexing: do IO and not wait for it to complete. ● But how would you know that the IO has completed? ● You either get a signal with signal handling problems. ● Or wait. But that is exactly what multiplexing does.
  • 7. The reactor pattern ● The reactor pattern in ACE and other frameworks is a pattern built on top of the OS specific multiplexing API. ● You write multiple objects with methods to handle various types of events. ● The framework then uses a single thread to wait for any of these events using the OS API. ● When the event happens the thread wakes up and calls the appropriate method on the appropriate object. ● And you get a nice C++ OO API so that you can easily write the event handlers independent of each other.
  • 8. File descriptors in UNIX ● File descriptors in UNIX were flexible to begin with. ● They could stand for: ● Regular file / folders ● Device driver ● Network socket ● Listening socket ● Anonymous or named pipes ● UNIX domain socket ● Virtual files, like /proc, which look like regular files ● So multiplexing on many file descriptors is already powerful.
  • 9. File descriptors in Linux ● In order to make multiplexing more flexible and effective the concept of file descriptor was enhanced in Linux to include things that were never in the UNIX definition. ● In Linux a file descriptor can also be: ● Timer (timerfd) ● Signal file descriptor for synchronous signal delivery. (signalfd) ● Event from another thread or process (eventfd)
  • 10. timerfd ● API: timerfd_create(2), timerfd_settime(2), timerfd_gettime(2) ● You create (open?!?) a timerfd by calling timerfd_create. ● You can say what type of clock (hardware, software, etc) your timer will be based on. ● You get a file descriptor. ● Now you can read(2) it and you block until the timer expires. ● Or you can multiplex on it.
  • 11. signalfd ● API: signalfd(2) ● You call signalfd(2) and create (open?!?) a file descriptor. ● You can specify what signals you want this file descriptor to catch and various flags. ● You can now read(2) this file descriptor and go to sleep and wait for any of the prescribed signals. ● Or you can multiplex on it.
  • 12. eventfd ● API: eventfd(2), eventfd_read(2), eventfd_write(2) ● Create (open?) an eventfd using eventfd(2) ● Now write or read from it. ● Writes and reads only work in 64 bit chunks. ● You can now read(2) this file descriptor and go to sleep to wait for this event. You will wake up once another thread does a write(2) to this file descriptor. ● Or you can multiplex on it.
  • 13. eventfd (cont) ● More efficient than a pipe (no circular buffer, messages are constant size). ● Simpler than a pipe (one fd and not two, no signals involved). ● Kernel uses this to signal user space (async io). ● You can use it for inter-thread or inter-process communication. ● For instance using fork.
  • 14. Linux multiplexing API ● Is comprised of three system calls (or system call groups): select, poll and epoll. ● They all work on file descriptors. ● Since file descriptor in Linux can stand for all the previously mentioned types multiplexing is very flexible. ● You pack file descriptors into a group and then you wait on the group.
  • 15. Why 3 different APIs? ● Because they evolved through time. ● First was select, then poll and then epoll. ● Differences are: size of group of fds, efficient handling of large groups of fds, timer support, signal masking and more. ● Epoll is the most advanced API so we will concentrate on that.
  • 16. epoll ● API: epoll(2), epoll_create(2), epoll_create1(2), epoll_ctl(2), epoll_pwait(2), epoll_wait(2) ● You create (open?) an epoll file descriptor using epoll_create(2). ● Now you can add/remove file descriptors to/from it using epoll_ctl. ● When adding a file descriptor you can say what you are waiting on this file descriptor for (read, write, error, etc).
  • 17. epoll (cont) ● Now you can epoll_wait(2) until something happens. ● When you wake up your array of epll_event structs is filled with details about what happened. ● You handle the events and go back to epoll_wait(2).
  • 18. epoll (cont) ● In epoll the list of file descriptors is held in kernel space. ● This is seen as one of epolls main strengths since passing a large file descriptor collection on each call to select(2) or poll(2) was problematic. ● This is what makes epoll(2) scale to large numbers of file descriptors (some report hundreds of thousands). ● Some see this as the pinnacle of Linux APIs.
  • 19. Multiplexing - conclusions ● Multiplexing in Linux is very powerful. ● You can do many many things in parallel in a single thread/process. ● It scales well to large numbers of file descriptors. ● The API is not easy but that is the kernel design. ● The idea is that it will be wrapped by user level libraries, maybe even OO. ● ACE needs to be compiled in a special way to support epoll.