Skip to content

Latest commit

 

History

History
53 lines (30 loc) · 3.02 KB

6.2 IO Models.md

File metadata and controls

53 lines (30 loc) · 3.02 KB

I/O Models

Five I/O models that are available to us under Unix:

  • blocking I/O
  • nonblocking I/O
  • I/O multiplexing (select and poll)
  • signal driven I/O (SIGIO)
  • asynchronous I/O (the POSIX aio_ functions)

There are normally two distinct phases for an input operation:

  1. Waiting for the data to be ready
  2. Copying the data from the kernel to the process

Blocking I/O Model

image-20230419002657933

Nonblocking I/O Model

image-20230419002802136

When an application sits in a loop calling recvfrom on a nonblocking descriptor like this, it is called polling. The application is continually polling the kernel to see if some operation is ready. This is often a waste of CPU time, but this model is occasionally encountered, normally on systems dedicated to one function.

I/O Multiplexing Model

image-20230419002940887

Comparing Figure 6.3 to Figure 6.1, there does not appear to be any advantage, and in fact, there is a slight disadvantage because using select requires two system calls instead of one. But the advantage in using select, which we will see later in this chapter, is that ==we can wait for more than one descriptor to be ready==.

Signal-Driven I/O Model

image-20230419003137323

Asynchronous I/O Model

In general, these functions work by telling the kernel to start the operation and to notify us when the entire operation (including the copy of the data from the kernel to our buffer) is complete. The main difference between this model and the signal-driven I/O model in the previous section is that ==with signal-driven I/O, the kernel tells us when an I/O operation can be initiated, but with asynchronous I/O, the kernel tells us when an I/O operation is complete.== We show an example in Figure 6.5.

image-20230419003247229

Comparison of the I/O Models

image-20230419003713596

Synchronous I/O versus Asynchronous I/O

POSIX defines these two terms as follows:

  • A synchronous I/O operation causes the requesting process to be blocked until that I/O operation completes.
  • An asynchronous I/O operation does not cause the requesting process to be blocked.

Using these definitions, the first four I/O models—blocking, nonblocking, I/O multiplexing, and signal-driven I/O—are all ==synchronous== because the actual I/O operation (recvfrom) blocks the process. Only the asynchronous I/O model matches the ==asynchronous== I/O definition.