Motivation
A web server needs to handle multiple clients simultaneously, with each client connection represented by a file descriptor. The challenge is efficiently monitoring all clients at once without wasting resources.
The Problem with Blocking I/O
With blocking I/O, the server calls read() on one client and gets stuck waiting for that specific client to send data. Meanwhile, other clients trying to send requests remain unread and unserviced. One idle client essentially freezes the entire server for everyone else.
The Problem with Non-blocking I/O
Non-blocking I/O solves the blocking issue by rapidly polling each client in a loop. However, each check requires an expensive system call that transitions between user and kernel mode. When clients have no data (the common case), the server wastes enormous CPU cycles repeatedly asking “ready yet?” to every client.
The Problem with Multi-Processes
Creating one process per client avoids both blocking and polling waste, but introduces IPC overhead. When processes need to share resources like database connections or coordinate actions, inter-process communication becomes expensive. Managing thousands of processes also consumes excessive memory and system resources.
I/O Multiplexing
Introduction
I/O multiplexing allows us to tell the kernel the operations that we want to do on certain file descriptors. Next, the kernel will help us monitor the resources about these file descriptors. Once one of the resource is free, the kernel will inform the process , the process can then do operations on the file
Benefits
Since we let the kernel help us monitor the status of the files, the process don’t need to waste CPU time to repeatedly trapped in kernel to check whether the resource is free
Note
System calls for I/O multiplexing such as select() and poll() are blocking. The process is put to sleep until timeout, thus during the system call the process can’t do other things
The benefit in comparison with nonblocking method is that it’ll not occupy the CPU and can let other processes to use CPU