SlideShare a Scribd company logo
1 of 7
The Linux streams library
Mark Veltzer
mark@veltzer.net
Streams and performance
● Since each FILE structure is held in user space
it could be accessed by two threads at the
same time.
● So the streams library locks each FILE to avoid
race conditions.
● You must take these locks into consideration
when designing a multi-threaded streams using
application.
Options for design – best option
● Don't use the same stream in different threads
● If you use the standard stream operation (without
the _unlocked suffix) then you get quite good
performance.
● This is due to the fact that uncontended locks in
Linux are acquired very fast in user space (futex).
● Or, you can go a step further and just use the
*_unlocked operations and get no locking at all.
Options for design – little contention
● If your application is not doing lots of IO then you
can just go ahead and use the same streams in
different threads with little contention.
● Remember that streams buffer in user space and
so most of the time you are causing very slight
delays (memcpy).
● In this case you must not use the _unlocked
versions of the functions.
Options for design – reducing
contention
● If you do contend on streams then you can
manage the locking yourself and get some more
performance.
● Use flockfile(3) and funlockfile(3) and between
them use the *_unlocked operations.
● If you have multiple IO operations between each
lock and unlock then you are saving the need to
acquire and release the lock multiple times.
Locking manually for transactional
IO
● If you have multiple threads that want to access the same
files using the streams library and you want each to perform
several IO operation without being interrupted then you must
resort to manual locking.
● Use flockfile(3) and funlockfile(3) and between them use the
*_unlocked operations.
● Note that this does not protect you from other processes
accessing the same file.
● If you put the FILE structures and their buffers (using
setbuffer) into shared memory you can use the streams
library for multi-processed designs.
References
● man unlocked_stdio from the Linux manual
pages
● 'I/O on streams' from 'info libc'.

More Related Content

Similar to Streams

Linux multiplexing
Linux multiplexingLinux multiplexing
Linux multiplexing
Mark Veltzer
 
LAS16-400: Mini Conference 3 AOSP (Session 1)
LAS16-400: Mini Conference 3 AOSP (Session 1)LAS16-400: Mini Conference 3 AOSP (Session 1)
LAS16-400: Mini Conference 3 AOSP (Session 1)
Linaro
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
ScyllaDB
 
Laporan Praktikum Keamanan Siber - Tugas 3 - Kelas C - Kelompok 3.pdf
Laporan Praktikum Keamanan Siber - Tugas 3 - Kelas C - Kelompok 3.pdfLaporan Praktikum Keamanan Siber - Tugas 3 - Kelas C - Kelompok 3.pdf
Laporan Praktikum Keamanan Siber - Tugas 3 - Kelas C - Kelompok 3.pdf
IGedeArieYogantaraSu
 
Lamp1
Lamp1Lamp1
Lamp1
Reka
 
Lamp
LampLamp
Lamp
Reka
 
Leveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IVLeveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IV
Opersys inc.
 

Similar to Streams (20)

Linux Locking Mechanisms
Linux Locking MechanismsLinux Locking Mechanisms
Linux Locking Mechanisms
 
Linux multiplexing
Linux multiplexingLinux multiplexing
Linux multiplexing
 
Arch linux and whole security concepts in linux explained
Arch linux and whole security concepts in linux explained Arch linux and whole security concepts in linux explained
Arch linux and whole security concepts in linux explained
 
LAS16-400: Mini Conference 3 AOSP (Session 1)
LAS16-400: Mini Conference 3 AOSP (Session 1)LAS16-400: Mini Conference 3 AOSP (Session 1)
LAS16-400: Mini Conference 3 AOSP (Session 1)
 
Linux 开源操作系统发展新趋势
Linux 开源操作系统发展新趋势Linux 开源操作系统发展新趋势
Linux 开源操作系统发展新趋势
 
Os Threads
Os ThreadsOs Threads
Os Threads
 
Distributed Elixir
Distributed ElixirDistributed Elixir
Distributed Elixir
 
The Tux 3 Linux Filesystem
The Tux 3 Linux FilesystemThe Tux 3 Linux Filesystem
The Tux 3 Linux Filesystem
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
 
Laporan Praktikum Keamanan Siber - Tugas 3 - Kelas C - Kelompok 3.pdf
Laporan Praktikum Keamanan Siber - Tugas 3 - Kelas C - Kelompok 3.pdfLaporan Praktikum Keamanan Siber - Tugas 3 - Kelas C - Kelompok 3.pdf
Laporan Praktikum Keamanan Siber - Tugas 3 - Kelas C - Kelompok 3.pdf
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
 
Lamp1
Lamp1Lamp1
Lamp1
 
Lamp1
Lamp1Lamp1
Lamp1
 
Lamp
LampLamp
Lamp
 
epoll() - The I/O Hero
epoll() - The I/O Heroepoll() - The I/O Hero
epoll() - The I/O Hero
 
Tips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development EfficiencyTips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development Efficiency
 
UCL All of the Things (MeetBSD California 2014 Lightning Talk)
UCL All of the Things (MeetBSD California 2014 Lightning Talk)UCL All of the Things (MeetBSD California 2014 Lightning Talk)
UCL All of the Things (MeetBSD California 2014 Lightning Talk)
 
Leveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IVLeveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IV
 
Multithreading in Scala
Multithreading in Scala Multithreading in Scala
Multithreading in Scala
 
Concept of thread
Concept of threadConcept of thread
Concept of thread
 

More from Mark Veltzer (10)

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
 
Pthreads linux
Pthreads linuxPthreads linux
Pthreads linux
 
Realtime
RealtimeRealtime
Realtime
 
Volatile
VolatileVolatile
Volatile
 
Effective cplusplus
Effective cplusplusEffective cplusplus
Effective cplusplus
 

Recently uploaded

+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...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

+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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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)
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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, ...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Streams

  • 1. The Linux streams library Mark Veltzer mark@veltzer.net
  • 2. Streams and performance ● Since each FILE structure is held in user space it could be accessed by two threads at the same time. ● So the streams library locks each FILE to avoid race conditions. ● You must take these locks into consideration when designing a multi-threaded streams using application.
  • 3. Options for design – best option ● Don't use the same stream in different threads ● If you use the standard stream operation (without the _unlocked suffix) then you get quite good performance. ● This is due to the fact that uncontended locks in Linux are acquired very fast in user space (futex). ● Or, you can go a step further and just use the *_unlocked operations and get no locking at all.
  • 4. Options for design – little contention ● If your application is not doing lots of IO then you can just go ahead and use the same streams in different threads with little contention. ● Remember that streams buffer in user space and so most of the time you are causing very slight delays (memcpy). ● In this case you must not use the _unlocked versions of the functions.
  • 5. Options for design – reducing contention ● If you do contend on streams then you can manage the locking yourself and get some more performance. ● Use flockfile(3) and funlockfile(3) and between them use the *_unlocked operations. ● If you have multiple IO operations between each lock and unlock then you are saving the need to acquire and release the lock multiple times.
  • 6. Locking manually for transactional IO ● If you have multiple threads that want to access the same files using the streams library and you want each to perform several IO operation without being interrupted then you must resort to manual locking. ● Use flockfile(3) and funlockfile(3) and between them use the *_unlocked operations. ● Note that this does not protect you from other processes accessing the same file. ● If you put the FILE structures and their buffers (using setbuffer) into shared memory you can use the streams library for multi-processed designs.
  • 7. References ● man unlocked_stdio from the Linux manual pages ● 'I/O on streams' from 'info libc'.