Blocking I/O vs Non-blocking I/O
About I/O
There are normally two distinct phases for an input operation:
Wait for the data to be ready
Copy the data from the kernel to the process
(For an input operation on a socket, the first step normally involves waiting for data to arrive on the network. When the packet arrives, it is copied into a buffer within the kernel. The second step is copying this data from the kernel's buffer into our application buffer.)
Blocking I/O
Where the system call, recvfrom
is implemented.
The process does not return until the data arrives and is copied into the application, or an error occurs.
Non-blocking I/O
When an I/O operation cannot be completed, instead of putting the process to sleep, it will return an error
Situation 1:
- call
recvfrom
- WHEN there is no data to return
- the kernel immediately returns an error of EWOULDBLOCK
- call
Situation 2:
- call
recvfrom
- WHEN a datagram is ready
- it is copied into our application buffer, and
recvfrom
returns successfully.
- call
When an application calling recvfrom
in a loop like this to check if the data is ready, it is called polling
Reference
http://www.masterraghu.com/subjects/np/introduction/unix_network_programming_v1.3/ch06lev1sec2.html