Blocking I/O vs Non-blocking I/O


About I/O

There are normally two distinct phases for an input operation:

  1. Wait for the data to be ready

  2. 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:

    1. call recvfrom
    2. WHEN there is no data to return
    3. the kernel immediately returns an error of EWOULDBLOCK
  • Situation 2:

    1. call recvfrom
    2. WHEN a datagram is ready
    3. it is copied into our application buffer, and recvfrom returns successfully.

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

results matching ""

    No results matching ""