NOTE
I/O Models
[toc] 1. Synchronous vs Asynchronous When an application invokes a system call, who handles the process? In the former, I ask you Synchronous means the application handles it itself. In the latter, you notify me Asynchronous means the operating syste
This is a historical learning note and may contain outdated or incomplete understanding.
1. Synchronous vs Asynchronous
- When an application invokes a system call, who handles the process?
In the former, I ask youSynchronous means the application handles it itself.In the latter, you notify meAsynchronous means the operating system handles it and notifies the application through a callback or event after completion.
2. Blocking vs Nonblocking
- When an application invokes a system call and there is no result yet, what happens?
- Blocking means the application waits.
- Nonblocking means the application returns immediately and then polls. Note that this has nothing to do with whether CPU is consumed. Disk/network -> kernel is a DMA copy and does not consume CPU, but this process is still blocking from the application’s perspective.
3. I/O Models
- If both disk or network <-> kernel space and kernel space <-> user space are nonblocking, it is asynchronous I/O.
- If kernel space <-> user space is blocking, it is synchronous I/O.
- Synchronous I/O is further classified according to whether disk or network <-> kernel space is blocking:
- Blocking:
- Synchronous blocking I/O
- Nonblocking:
- User-space polling: synchronous nonblocking I/O
- Kernel-space polling: I/O multiplexing
- Operating-system callback: signal-driven I/O
- Blocking:
- Synchronous I/O is further classified according to whether disk or network <-> kernel space is blocking:
| Synchronous Blocking I/O | Synchronous Nonblocking I/O | I/O Multiplexing | Signal-Driven I/O | Asynchronous I/O | |
|---|---|---|---|---|---|
| Kernel space <-> user space | Blocking | Blocking | Blocking | Blocking | Nonblocking |
| Disk or network <-> kernel space | Blocking | Nonblocking + application polling | Nonblocking + kernel-space polling | Nonblocking + callback | Nonblocking |
3.1. Synchronous Blocking I/O
- The application calls read and write functions.
- If used for network I/O, multiple threads are needed to serve multiple users, but threads cannot be created without limit.
3.2. Synchronous Nonblocking I/O
- The application calls read and write functions + the O_NONBLOCK parameter.
- If used for network I/O, one thread can serve multiple users, but because system calls are polled, the CPU switches between kernel mode and user mode, consuming a lot of CPU.
3.3. I/O Multiplexing
- The application process calls select, poll, or epoll.
- If used for network I/O, one thread can serve multiple users. When an I/O event occurs, the I/O multiplexer notifies the upper-layer application to process it, rather than the upper-layer application actively polling, so it does not cause frequent CPU switches between kernel mode and user mode.
3.3.1. Reactor Programming Model
A type of event-driven programming. The process of network-service programming is:
- accept
- read/write socket
- decode/encode + serialize/deserialize + logic Single-Reactor single-thread model: all three operations are executed in one thread. For example, Redis. Single-Reactor multi-thread model: the first two operations are executed in one thread, and the last operation is executed through multiple threads. Multi-Reactor multi-thread model: the first operation is executed in one thread, the second operation is executed in as many threads as there are CPU cores, and the last operation is executed through multiple threads. For example, Netty. Reactor Thread Model - Zhihu Understand the Reactor Model in Five Minutes - CSDN
3.4. Signal-Driven I/O
3.5. Asynchronous I/O
- The application process calls Linux aio or Windows IOCP.

Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub