I/O Models
Unix I/O 模型說明
I/O Models
- Synchronous IO
Blocking I/OModel (Blocking / Synchronous) : BIO, 阻塞 IONon-Blocking I/O(Non-Blocking / Synchronous) : NIO, 非阻塞 IOI/O Multiplexing(Blocking / Asynchronous): I/O 多工, 多路複用 IOSignal-driven I/O(SIGIO): 訊號驅動,屬於 Synchronous I/O, 訊號驅動 IO
Asynchronous I/O(Non-Blocking / Asynchronous): AIO, 異步 IO

BIO
sequenceDiagram
participant App as Application
participant K as Kernel
Note over App: process blocks in a call to recvfrom
App ->> K: recvfrom - system call
K ->> K: no datagram ready
Note over K: wait for data
K ->> K: no datagram ready
Note over K: wait for data
K ->> K: datagram ready
K ->> K: copy datagram
K ->> K: copy complete
Note over K: copy data from kernel to user
K ->> App: return OK
App ->> App: process datagram
NIO
sequenceDiagram
participant App as Application
participant K as Kernel
Note over App: process repeatedly calls recvfrom, waiting for an OK return (polling)
App ->> K: recvfrom - system call
K ->> K: no datagram ready
K ->> App: EWOULDBLOCK
App ->> K: recvfrom - system call
K ->> K: no datagram ready
K ->> App: EWOULDBLOCK
App ->> K: recvfrom - system call
K ->> K: no datagram ready
K ->> App: EWOULDBLOCK
Note right of K: wait for data
App ->> K: recvfrom - system call
K ->> K: datagram ready
Note right of K: copy data from kernel to user
K ->> K: copy datagram
K ->> K: copy complete
K ->> App: return OK
App ->> App: process datagram
IO Multiplexing
sequenceDiagram
participant App as Application
participant K as Kernel
Note over App: process repeatedly calls select, waiting for one possibly many sockets to become readable
App ->> K: select - system call
K ->> K: no datagram ready
Note over K: wait for data
K ->> K: datagram ready
K ->> App: return readable
Note over App: process repeatedly calls select, waiting for one possibly many sockets to become readable
App ->> K: recvfrom - system call
K ->> K: copy datagram
Note right of K: copy data from kernel to user
K ->> K: copy complete
K ->> App: return OK
App ->> App: process datagram
Signal-Driven IO
sequenceDiagram
participant App as Application
participant K as Kernel
Note over K: wait for data
App ->> App: establish SIGIO signal handler
App ->> K: sigaction - system call
K ->> App: return
Note over App: process continues executing
K ->> K: datagram ready
Note over App: process blocks while data copied into application buffer
Note left of App: signal handler
K ->> App: deliver SIGIO
App ->> K: recvfrom - system call
K ->> K: copy datagram
Note right of K: copy data from kernel to user
K ->> K: copy complete
K ->> App: return OK
App ->> App: process datagram
AIO
sequenceDiagram
participant App as Application
participant K as Kernel
Note over K: process continues executing
App ->> K: aio_read - system call
Note over App: wait for data
K ->> K: no datagram ready
K ->> App: return
K ->> K: datagram ready
Note right of K: copy data from kernel to user
K ->> K: copy complete
K ->> App: deliver signal specified in aio_read
Note over App: signal handler process datagram
Network IO
輸入輸出 (IO) 操作的核心概念在於 User Space Buffer 與 Kernel Space Buffer 之間的互相複製數據。
它通常包含以下兩個步驟:
- 等待網路數據到達網卡 (NIC) (read-ready) / 等待網卡 (NIC) Writable (write-ready)
→ read / write 到
Kernel Space Buffer - 從
Kernel Space Buffer複製數據到User Space Buffer(read) 從User Space Buffer複製數據到Kernel Space Buffer(write)