The five models range from most blocking to least, and from lowest to highest efficiency:
- Blocking I/O: Traditional model.
- Non-blocking I/O: Different from Java NIO (New IO).
- I/O Multiplexing: Reactor pattern; uses selectors like Linux's epoll.
- Signal-driven I/O.
- Asynchronous I/O: Proactor pattern, also known as non-blocking asynchronous I/O.
Unix Input Operations
A Unix input operation consists of two phases:
- Waiting for data to be ready.
- Copying data from the kernel to the process buffer.
For a socket input operation: - Data arrives over the network and is copied into a kernel buffer.
- Datta is then copied from the kernel buffer to the application buffer.
Blocking I/O
In blocking I/O, an application call blocks untill the data is ready. Here’s a simplified flow:
// Example code in pseudo-code
function readData(socket) {
if (!isDataReady(socket)) {
wait(); // Block until data is ready
}
copyDataFromKernelToUser();
}
Non-blocking I/O Model
Non-blocking I/O sets sockets to return immediately with an error if data isn't ready:
function pollData(socket) {
while (!isDataReady(socket)) {
checkStatus(); // Poll continuously
}
copyDataFromKernelToUser();
}
I/O Multiplexing Model
This model monitors multiple descriptors, notifying when any are ready for reading or writing:
function monitorSockets(sockets) {
select(sockets); // Blocks until one or more sockets are ready
foreach (readySocket in sockets) {
readOrWrite(readySocket);
}
}
Signal-driven I/O Model
Uses signals to notify when a descriptor is ready:
setupSignalHandler(socket);
// Process continues running, no block
onSignal(SIGIO) {
readData(socket);
}
Asynchronous I/O Model
Initiates operations that notify completion:
aio_read(socket, buffer, size, offset, notificationMethod);
// Process does other work
onCompletion() {
processData(buffer);
}
Comparison of Blocking vs Non-blocking I/O
Blocking I/O waits until operations complete, whereas non-blocking returns immediately if not ready.
Synchronous vs Asynchronous I/O
Synchronous I/O blocks until operations complete, while asynchronous I/O allows the process to continue without waiting.
Comparative Analysis of I/O Models
Each model differs mainly in how they handle readiness checks and data copying.