SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Linux IO
1
Liran Ben Haim
liran@discoversdk.com
Rights to Copy
 Attribution – ShareAlike 2.0
 You are free
to copy, distribute, display, and perform the work
to make derivative works
to make commercial use of the work
Under the following conditions
Attribution. You must give the original author credit.
Share Alike. If you alter, transform, or build upon this work, you may
distribute the resulting work only under a license identical to this one.
For any reuse or distribution, you must make clear to others the license
terms of this work.
Any of these conditions can be waived if you get permission from the
copyright holder.
Your fair use and other rights are in no way affected by the above.
License text: http://creativecommons.org/licenses/by-sa/2.0/legalcode
 This kit contains work by the
following authors:
 © Copyright 2004-2009
Michael Opdenacker /Free
Electrons
michael@free-electrons.com
http://www.free-electrons.com
 © Copyright 2003-2006
Oron Peled
oron@actcom.co.il
http://www.actcom.co.il/~oron
 © Copyright 2004–2008
Codefidence ltd.
info@codefidence.com
http://www.codefidence.com
 © Copyright 2009–2010
Bina ltd.
info@bna.co.il
http://www.bna.co.il
2
Typical System
3
IO Models
 Blocking I/O
 Non-blocking I/O
 I/O multiplexing (select/poll/…)
 Signal driven I/O (SIGIO)
 Asynchronous I/O
 aio_* functions
 io_* functions
4
Blocking IO
 Default mode
 Makes the calling thread to block in the kernel in case
no data is available
 Can block forever
 To block with timeout, use select
5
Non Blocking IO
 If there is no data, calling thread returns with EAGAIN
or EWOULDBLOCK
flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
 To use in your own driver
6
IO Multiplexing
 With I/O multiplexing, we call select/poll/epoll* and
block in one of these system calls, instead of blocking
in the actual I/O system call
 Disadvantage: using select requires at least two system
calls (select and recvfrom) instead of one
 Advantage: we can wait for more than one descriptor to
be ready
7
Signal driven I/O
 The signal-driven I/O model uses signals, telling the kernel
to notify us with the SIGIO signal when the descriptor is
ready
fd=open(“name”, O_NONBLOCK);
fcntl(fd, F_SETSIG, SIGRTMIN + 1);
 Its better to use RT signals (queued)
 To use in your own driver
 if (file->f_flags & O_NONBLOCK)
 Send signal to file->f_owner (fown_struct)
 signum
 pid
8
Asynchronous IO
 The POSIX asynchronous I/O interface
 Allows applications to initiate one or more I/O operations
that are performed asynchronously.
 The application can select to be notified of completion of
the I/O operation in a variety of ways:
 Delivery of a signal
 Instantiation of a thread
 No notification at all
 User space asynchronous implementation
 Call the driver read/write callbacks
9
Kernel Async Support
 file_operations callbacks
 aio_read, aio_write
 Initialize the data processing
 Create workqueue item/completion/timer/…
 On completion
 aio_complete
 Kernel 3.16
 read_iter
 write_iter
 To use from user space call io_submit
10
I/O Multiplexing
 select, pselect
 poll, ppoll
 epoll
 epoll_create, epoll_create1
 epoll_ctl
 epoll_wait, epoll_pwait
11
select(2) system call
The select( ) system call provides a mechanism for implementing synchronous
multiplexing I/O
A call to select( ) will block until the given file descriptors are ready to perform
I/O, or until an optionally specified timeout has elapsed
The watched file descriptors are broken into three sets
File descriptors listed in the readfds set are watched to see if data is
available for reading.
File descriptors listed in the writefds set are watched to see if a write
operation will complete without blocking.
File descriptors in the exceptfds set are watched to see if an exception has
occurred, or if out-of-band data is available (these states apply only to
sockets).
A given set may be NULL, in which case select( ) does not watch for that event.
On successful return, each set is modified such that it contains only the file
descriptors that are ready for I/O of the type delineated by that set
Blocking for events
You can use select(2) to block for events
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout);
nfds: number of highest file descriptor + 1
fd_sets: sets of file descriptors to block for events on,
one for read, one for write, one for exceptions.
timeout: NULL or structure describing how long to
block.
Linux updates the timeout structure according to how
much time was left to the time out.
File Descriptor Sets
fd_set is a group of file descriptor for actions or
reports:
void FD_CLR(int fd, fd_set *set);
Remove fd from this set.
int FD_ISSET(int fd, fd_set *set);
Is fd in the set?
void FD_SET(int fd, fd_set *set);
Add fd to this set.
void FD_ZERO(fd_set *set);
Zero the set.
Driver notification example
int fd1, fd2;
fd_set fds_set;
int ret;
fd1 = open(“/dev/drv_ctl0”, O_RDWR);
fd2 = open(“/dev/drv_ctl1”, O_RDWR);
FD_ZERO(&fds_set);
FD_SET(fd1, &fds_set);
FD_SET(fd2, &fds_set);
do {
ret = select(fd1 + fd2 + 1, &fds_set, NULL, NULL,
NULL);
} while(errno == EINTR);
Open two file descriptors
fd1 and fd2.
Create an fd set that
holds them.
Block for events on them.
We try again if we
interrupted by a signal.
Driver notification example 2
if(ret == -1) {
perror("Select failed.");
exit(1);
}
if(FD_ISSET(fd1, &fds_set)) {
printf("event at FD 1... “);
ioctl(fd1, IOCTL_CLR, 1);
printf("clearn);
}
If we have an error bail
out.
Check if fd1 one has a
pending read even.
If so, use ioctl(2) to
notify driver to clear
status
pselect
POSIX implementation
Does not modify the timeout parameter
Can set signal mask before entering to sleep
Uses the timespec structure (seconds, nanoseconds)
Poll
 int poll (struct pollfd *fds, unsigned int nfds, int timeout);
Unlike select(), with its inefficient three bitmask-based sets of file descriptors, poll( )
employs a single array of nfds pollfd structures
#include <sys/poll.h>
struct pollfd {
int fd;
short events;
short revents;
};
POSIX implementation – ppoll (with signal mask)
19https://www.ulduzsoft.com/2014/01/select-poll-epoll-practical-difference-for-system-architects/
Poll vs. Select
poll( ) does not require that the user calculate the value of the highest-
numbered file descriptor +1
poll( ) is more efficient for large-valued file descriptors. Imagine watching a
single file descriptor with the value 900 via select()—the kernel would have
to check each bit of each passed-in set, up to the 900th bit.
select( )’s file descriptor sets are statically sized.
With select( ), the file descriptor sets are reconstructed on return, so each
subsequent call must reinitialize them. The poll( ) system call separates the
input (events field) from the output (revents field), allowing the array to be
reused without change.
The timeout parameter to select( ) is undefined on return. Portable code
needs to reinitialize it. This is not an issue with pselect( )
select( ) is more portable, as some Unix systems do not support poll( ).
Event Poll
Has state in the kernel
O(1) instead of O(n)
epoll_create(2) – initializes epoll context
epoll_ctl(2) – adds/removes file descriptors from the
context
epoll_wait(2) – performs the actual event wait
Can behave as
Edge triggered
Level triggered
Example
int epfd = epoll_create(0);
int client_sock = socket(…..);
static struct epoll_event ev;
ev.events = EPOLLIN | EPOLLOUT | EPOLLERR;
ev.data.fd = client_sock;
int res = epoll_ctl(epfd, EPOLL_CTL_ADD, client_sock, &ev);
struct epoll_event *events = malloc(SIZE);
while (1) {
int nfds = epoll_wait(epfd, events, MAX_EVENTS, TIMEOUT);
for(int i = 0; i < nfds; i++) {
int fd = events[i].data.fd;
handle_io_on_socket(fd);
}
}
Driver Implementation
 Implement poll callback on file_operations
 The driver adds a wait queue to the poll_table structure
by calling the function poll_wait
23
Thank You
Code examples and more
http://www.discoversdk.com/blog
24

Weitere ähnliche Inhalte

Was ist angesagt?

Linux SMEP bypass techniques
Linux SMEP bypass techniquesLinux SMEP bypass techniques
Linux SMEP bypass techniquesVitaly Nikolenko
 
Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems Ahmed El-Arabawy
 
PostgreSQL + ZFS best practices
PostgreSQL + ZFS best practicesPostgreSQL + ZFS best practices
PostgreSQL + ZFS best practicesSean Chittenden
 
How Linux Processes Your Network Packet - Elazar Leibovich
How Linux Processes Your Network Packet - Elazar LeibovichHow Linux Processes Your Network Packet - Elazar Leibovich
How Linux Processes Your Network Packet - Elazar LeibovichDevOpsDays Tel Aviv
 
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_uringScyllaDB
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Scriptsbmguys
 
[오픈소스컨설팅]RHEL7/CentOS7 Pacemaker기반-HA시스템구성-v1.0
[오픈소스컨설팅]RHEL7/CentOS7 Pacemaker기반-HA시스템구성-v1.0[오픈소스컨설팅]RHEL7/CentOS7 Pacemaker기반-HA시스템구성-v1.0
[오픈소스컨설팅]RHEL7/CentOS7 Pacemaker기반-HA시스템구성-v1.0Ji-Woong Choi
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageKernel TLV
 
NEDIA_SNIA_CXL_講演資料.pdf
NEDIA_SNIA_CXL_講演資料.pdfNEDIA_SNIA_CXL_講演資料.pdf
NEDIA_SNIA_CXL_講演資料.pdfYasunori Goto
 
Understand and optimize Linux I/O
Understand and optimize Linux I/OUnderstand and optimize Linux I/O
Understand and optimize Linux I/OAndrea Righi
 
Linux Crash Dump Capture and Analysis
Linux Crash Dump Capture and AnalysisLinux Crash Dump Capture and Analysis
Linux Crash Dump Capture and AnalysisPaul V. Novarese
 
HKG15-107: ACPI Power Management on ARM64 Servers (v2)
HKG15-107: ACPI Power Management on ARM64 Servers (v2)HKG15-107: ACPI Power Management on ARM64 Servers (v2)
HKG15-107: ACPI Power Management on ARM64 Servers (v2)Linaro
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingAnil Kumar Pugalia
 
LCE13: Android Graphics Upstreaming
LCE13: Android Graphics UpstreamingLCE13: Android Graphics Upstreaming
LCE13: Android Graphics UpstreamingLinaro
 
The Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsThe Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsDivye Kapoor
 
Design and Implementation of GCC Register Allocation
Design and Implementation of GCC Register AllocationDesign and Implementation of GCC Register Allocation
Design and Implementation of GCC Register AllocationKito Cheng
 
DMA Survival Guide
DMA Survival GuideDMA Survival Guide
DMA Survival GuideKernel TLV
 

Was ist angesagt? (20)

Linux SMEP bypass techniques
Linux SMEP bypass techniquesLinux SMEP bypass techniques
Linux SMEP bypass techniques
 
Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems
 
PostgreSQL + ZFS best practices
PostgreSQL + ZFS best practicesPostgreSQL + ZFS best practices
PostgreSQL + ZFS best practices
 
Linux
LinuxLinux
Linux
 
How Linux Processes Your Network Packet - Elazar Leibovich
How Linux Processes Your Network Packet - Elazar LeibovichHow Linux Processes Your Network Packet - Elazar Leibovich
How Linux Processes Your Network Packet - Elazar Leibovich
 
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
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
 
[오픈소스컨설팅]RHEL7/CentOS7 Pacemaker기반-HA시스템구성-v1.0
[오픈소스컨설팅]RHEL7/CentOS7 Pacemaker기반-HA시스템구성-v1.0[오픈소스컨설팅]RHEL7/CentOS7 Pacemaker기반-HA시스템구성-v1.0
[오픈소스컨설팅]RHEL7/CentOS7 Pacemaker기반-HA시스템구성-v1.0
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast Storage
 
NEDIA_SNIA_CXL_講演資料.pdf
NEDIA_SNIA_CXL_講演資料.pdfNEDIA_SNIA_CXL_講演資料.pdf
NEDIA_SNIA_CXL_講演資料.pdf
 
Understand and optimize Linux I/O
Understand and optimize Linux I/OUnderstand and optimize Linux I/O
Understand and optimize Linux I/O
 
Linux Crash Dump Capture and Analysis
Linux Crash Dump Capture and AnalysisLinux Crash Dump Capture and Analysis
Linux Crash Dump Capture and Analysis
 
HKG15-107: ACPI Power Management on ARM64 Servers (v2)
HKG15-107: ACPI Power Management on ARM64 Servers (v2)HKG15-107: ACPI Power Management on ARM64 Servers (v2)
HKG15-107: ACPI Power Management on ARM64 Servers (v2)
 
Linux User Space Debugging & Profiling
Linux User Space Debugging & ProfilingLinux User Space Debugging & Profiling
Linux User Space Debugging & Profiling
 
LCE13: Android Graphics Upstreaming
LCE13: Android Graphics UpstreamingLCE13: Android Graphics Upstreaming
LCE13: Android Graphics Upstreaming
 
The Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsThe Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOs
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
Design and Implementation of GCC Register Allocation
Design and Implementation of GCC Register AllocationDesign and Implementation of GCC Register Allocation
Design and Implementation of GCC Register Allocation
 
Linux Internals - Part II
Linux Internals - Part IILinux Internals - Part II
Linux Internals - Part II
 
DMA Survival Guide
DMA Survival GuideDMA Survival Guide
DMA Survival Guide
 

Andere mochten auch

Windows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel DevelopersWindows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel DevelopersKernel TLV
 
Programming Embedded linux
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linuxLiran Ben Haim
 
VLANs in the Linux Kernel
VLANs in the Linux KernelVLANs in the Linux Kernel
VLANs in the Linux KernelKernel TLV
 
FD.IO Vector Packet Processing
FD.IO Vector Packet ProcessingFD.IO Vector Packet Processing
FD.IO Vector Packet ProcessingKernel TLV
 
Linux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use CasesLinux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use CasesKernel TLV
 
Userfaultfd and Post-Copy Migration
Userfaultfd and Post-Copy MigrationUserfaultfd and Post-Copy Migration
Userfaultfd and Post-Copy MigrationKernel TLV
 
WiFi and the Beast
WiFi and the BeastWiFi and the Beast
WiFi and the BeastKernel TLV
 
Linux Kernel Init Process
Linux Kernel Init ProcessLinux Kernel Init Process
Linux Kernel Init ProcessKernel TLV
 
Fun with Network Interfaces
Fun with Network InterfacesFun with Network Interfaces
Fun with Network InterfacesKernel TLV
 
Debugging the Deadlock for the Scheduler
Debugging the Deadlock for the SchedulerDebugging the Deadlock for the Scheduler
Debugging the Deadlock for the SchedulerAmit Banerjee
 
Testing real-time Linux. What to test and how
Testing real-time Linux. What to test and how Testing real-time Linux. What to test and how
Testing real-time Linux. What to test and how Chirag Jog
 
Hardware Probing in the Linux Kernel
Hardware Probing in the Linux KernelHardware Probing in the Linux Kernel
Hardware Probing in the Linux KernelKernel TLV
 
Switchdev - No More SDK
Switchdev - No More SDKSwitchdev - No More SDK
Switchdev - No More SDKKernel TLV
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux InterruptsKernel TLV
 
Kernel overview
Kernel overviewKernel overview
Kernel overviewKai Sasaki
 

Andere mochten auch (20)

Windows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel DevelopersWindows Internals for Linux Kernel Developers
Windows Internals for Linux Kernel Developers
 
Programming Embedded linux
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linux
 
Android internals
Android internalsAndroid internals
Android internals
 
Linux internals v4
Linux internals v4Linux internals v4
Linux internals v4
 
VLANs in the Linux Kernel
VLANs in the Linux KernelVLANs in the Linux Kernel
VLANs in the Linux Kernel
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
React native
React nativeReact native
React native
 
FD.IO Vector Packet Processing
FD.IO Vector Packet ProcessingFD.IO Vector Packet Processing
FD.IO Vector Packet Processing
 
Linux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use CasesLinux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use Cases
 
Linux interrupts
Linux interruptsLinux interrupts
Linux interrupts
 
Userfaultfd and Post-Copy Migration
Userfaultfd and Post-Copy MigrationUserfaultfd and Post-Copy Migration
Userfaultfd and Post-Copy Migration
 
WiFi and the Beast
WiFi and the BeastWiFi and the Beast
WiFi and the Beast
 
Linux Kernel Init Process
Linux Kernel Init ProcessLinux Kernel Init Process
Linux Kernel Init Process
 
Fun with Network Interfaces
Fun with Network InterfacesFun with Network Interfaces
Fun with Network Interfaces
 
Debugging the Deadlock for the Scheduler
Debugging the Deadlock for the SchedulerDebugging the Deadlock for the Scheduler
Debugging the Deadlock for the Scheduler
 
Testing real-time Linux. What to test and how
Testing real-time Linux. What to test and how Testing real-time Linux. What to test and how
Testing real-time Linux. What to test and how
 
Hardware Probing in the Linux Kernel
Hardware Probing in the Linux KernelHardware Probing in the Linux Kernel
Hardware Probing in the Linux Kernel
 
Switchdev - No More SDK
Switchdev - No More SDKSwitchdev - No More SDK
Switchdev - No More SDK
 
Linux Interrupts
Linux InterruptsLinux Interrupts
Linux Interrupts
 
Kernel overview
Kernel overviewKernel overview
Kernel overview
 

Ähnlich wie Linux IO

Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式艾鍗科技
 
Kqueue : Generic Event notification
Kqueue : Generic Event notificationKqueue : Generic Event notification
Kqueue : Generic Event notificationMahendra M
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device driversAlexandre Moreno
 
Linux or unix interview questions
Linux or unix interview questionsLinux or unix interview questions
Linux or unix interview questionsTeja Bheemanapally
 
Unix.system.calls
Unix.system.callsUnix.system.calls
Unix.system.callsGRajendra
 
Linux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OLinux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OYourHelper1
 
Working with the IFS on System i
Working with the IFS on System iWorking with the IFS on System i
Working with the IFS on System iChuck Walker
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]mcganesh
 
All'ombra del Leviatano: Filesystem in Userspace
All'ombra del Leviatano: Filesystem in UserspaceAll'ombra del Leviatano: Filesystem in Userspace
All'ombra del Leviatano: Filesystem in UserspaceRoberto Reale
 
How To Add System Call In Ubuntu OS
How To Add System Call In Ubuntu OSHow To Add System Call In Ubuntu OS
How To Add System Call In Ubuntu OSPratik Tambekar
 
Basics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubBasics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubDevang Garach
 

Ähnlich wie Linux IO (20)

Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式Linux 系統程式--第一章 i/o 函式
Linux 系統程式--第一章 i/o 函式
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Kqueue : Generic Event notification
Kqueue : Generic Event notificationKqueue : Generic Event notification
Kqueue : Generic Event notification
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
Linux
LinuxLinux
Linux
 
Linux or unix interview questions
Linux or unix interview questionsLinux or unix interview questions
Linux or unix interview questions
 
Unix.system.calls
Unix.system.callsUnix.system.calls
Unix.system.calls
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
Linux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OLinux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/O
 
Working with the IFS on System i
Working with the IFS on System iWorking with the IFS on System i
Working with the IFS on System i
 
Sockets and Socket-Buffer
Sockets and Socket-BufferSockets and Socket-Buffer
Sockets and Socket-Buffer
 
Linux security
Linux securityLinux security
Linux security
 
Linux
LinuxLinux
Linux
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
 
All'ombra del Leviatano: Filesystem in Userspace
All'ombra del Leviatano: Filesystem in UserspaceAll'ombra del Leviatano: Filesystem in Userspace
All'ombra del Leviatano: Filesystem in Userspace
 
How To Add System Call In Ubuntu OS
How To Add System Call In Ubuntu OSHow To Add System Call In Ubuntu OS
How To Add System Call In Ubuntu OS
 
Unix-module3.pptx
Unix-module3.pptxUnix-module3.pptx
Unix-module3.pptx
 
Linux
LinuxLinux
Linux
 
Basics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubBasics of Linux Commands, Git and Github
Basics of Linux Commands, Git and Github
 

Kürzlich hochgeladen

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 

Kürzlich hochgeladen (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

Linux IO

  • 1. Linux IO 1 Liran Ben Haim liran@discoversdk.com
  • 2. Rights to Copy  Attribution – ShareAlike 2.0  You are free to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions Attribution. You must give the original author credit. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. License text: http://creativecommons.org/licenses/by-sa/2.0/legalcode  This kit contains work by the following authors:  © Copyright 2004-2009 Michael Opdenacker /Free Electrons michael@free-electrons.com http://www.free-electrons.com  © Copyright 2003-2006 Oron Peled oron@actcom.co.il http://www.actcom.co.il/~oron  © Copyright 2004–2008 Codefidence ltd. info@codefidence.com http://www.codefidence.com  © Copyright 2009–2010 Bina ltd. info@bna.co.il http://www.bna.co.il 2
  • 4. IO Models  Blocking I/O  Non-blocking I/O  I/O multiplexing (select/poll/…)  Signal driven I/O (SIGIO)  Asynchronous I/O  aio_* functions  io_* functions 4
  • 5. Blocking IO  Default mode  Makes the calling thread to block in the kernel in case no data is available  Can block forever  To block with timeout, use select 5
  • 6. Non Blocking IO  If there is no data, calling thread returns with EAGAIN or EWOULDBLOCK flags = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flags | O_NONBLOCK);  To use in your own driver 6
  • 7. IO Multiplexing  With I/O multiplexing, we call select/poll/epoll* and block in one of these system calls, instead of blocking in the actual I/O system call  Disadvantage: using select requires at least two system calls (select and recvfrom) instead of one  Advantage: we can wait for more than one descriptor to be ready 7
  • 8. Signal driven I/O  The signal-driven I/O model uses signals, telling the kernel to notify us with the SIGIO signal when the descriptor is ready fd=open(“name”, O_NONBLOCK); fcntl(fd, F_SETSIG, SIGRTMIN + 1);  Its better to use RT signals (queued)  To use in your own driver  if (file->f_flags & O_NONBLOCK)  Send signal to file->f_owner (fown_struct)  signum  pid 8
  • 9. Asynchronous IO  The POSIX asynchronous I/O interface  Allows applications to initiate one or more I/O operations that are performed asynchronously.  The application can select to be notified of completion of the I/O operation in a variety of ways:  Delivery of a signal  Instantiation of a thread  No notification at all  User space asynchronous implementation  Call the driver read/write callbacks 9
  • 10. Kernel Async Support  file_operations callbacks  aio_read, aio_write  Initialize the data processing  Create workqueue item/completion/timer/…  On completion  aio_complete  Kernel 3.16  read_iter  write_iter  To use from user space call io_submit 10
  • 11. I/O Multiplexing  select, pselect  poll, ppoll  epoll  epoll_create, epoll_create1  epoll_ctl  epoll_wait, epoll_pwait 11
  • 12. select(2) system call The select( ) system call provides a mechanism for implementing synchronous multiplexing I/O A call to select( ) will block until the given file descriptors are ready to perform I/O, or until an optionally specified timeout has elapsed The watched file descriptors are broken into three sets File descriptors listed in the readfds set are watched to see if data is available for reading. File descriptors listed in the writefds set are watched to see if a write operation will complete without blocking. File descriptors in the exceptfds set are watched to see if an exception has occurred, or if out-of-band data is available (these states apply only to sockets). A given set may be NULL, in which case select( ) does not watch for that event. On successful return, each set is modified such that it contains only the file descriptors that are ready for I/O of the type delineated by that set
  • 13. Blocking for events You can use select(2) to block for events int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); nfds: number of highest file descriptor + 1 fd_sets: sets of file descriptors to block for events on, one for read, one for write, one for exceptions. timeout: NULL or structure describing how long to block. Linux updates the timeout structure according to how much time was left to the time out.
  • 14. File Descriptor Sets fd_set is a group of file descriptor for actions or reports: void FD_CLR(int fd, fd_set *set); Remove fd from this set. int FD_ISSET(int fd, fd_set *set); Is fd in the set? void FD_SET(int fd, fd_set *set); Add fd to this set. void FD_ZERO(fd_set *set); Zero the set.
  • 15. Driver notification example int fd1, fd2; fd_set fds_set; int ret; fd1 = open(“/dev/drv_ctl0”, O_RDWR); fd2 = open(“/dev/drv_ctl1”, O_RDWR); FD_ZERO(&fds_set); FD_SET(fd1, &fds_set); FD_SET(fd2, &fds_set); do { ret = select(fd1 + fd2 + 1, &fds_set, NULL, NULL, NULL); } while(errno == EINTR); Open two file descriptors fd1 and fd2. Create an fd set that holds them. Block for events on them. We try again if we interrupted by a signal.
  • 16. Driver notification example 2 if(ret == -1) { perror("Select failed."); exit(1); } if(FD_ISSET(fd1, &fds_set)) { printf("event at FD 1... “); ioctl(fd1, IOCTL_CLR, 1); printf("clearn); } If we have an error bail out. Check if fd1 one has a pending read even. If so, use ioctl(2) to notify driver to clear status
  • 17. pselect POSIX implementation Does not modify the timeout parameter Can set signal mask before entering to sleep Uses the timespec structure (seconds, nanoseconds)
  • 18. Poll  int poll (struct pollfd *fds, unsigned int nfds, int timeout); Unlike select(), with its inefficient three bitmask-based sets of file descriptors, poll( ) employs a single array of nfds pollfd structures #include <sys/poll.h> struct pollfd { int fd; short events; short revents; }; POSIX implementation – ppoll (with signal mask)
  • 20. Poll vs. Select poll( ) does not require that the user calculate the value of the highest- numbered file descriptor +1 poll( ) is more efficient for large-valued file descriptors. Imagine watching a single file descriptor with the value 900 via select()—the kernel would have to check each bit of each passed-in set, up to the 900th bit. select( )’s file descriptor sets are statically sized. With select( ), the file descriptor sets are reconstructed on return, so each subsequent call must reinitialize them. The poll( ) system call separates the input (events field) from the output (revents field), allowing the array to be reused without change. The timeout parameter to select( ) is undefined on return. Portable code needs to reinitialize it. This is not an issue with pselect( ) select( ) is more portable, as some Unix systems do not support poll( ).
  • 21. Event Poll Has state in the kernel O(1) instead of O(n) epoll_create(2) – initializes epoll context epoll_ctl(2) – adds/removes file descriptors from the context epoll_wait(2) – performs the actual event wait Can behave as Edge triggered Level triggered
  • 22. Example int epfd = epoll_create(0); int client_sock = socket(…..); static struct epoll_event ev; ev.events = EPOLLIN | EPOLLOUT | EPOLLERR; ev.data.fd = client_sock; int res = epoll_ctl(epfd, EPOLL_CTL_ADD, client_sock, &ev); struct epoll_event *events = malloc(SIZE); while (1) { int nfds = epoll_wait(epfd, events, MAX_EVENTS, TIMEOUT); for(int i = 0; i < nfds; i++) { int fd = events[i].data.fd; handle_io_on_socket(fd); } }
  • 23. Driver Implementation  Implement poll callback on file_operations  The driver adds a wait queue to the poll_table structure by calling the function poll_wait 23
  • 24. Thank You Code examples and more http://www.discoversdk.com/blog 24