"Network Programming" I/O model

Source: Internet
Author: User

Objective

Before analyzing the I/O model, first understand the basic concepts of synchronous I/O and asynchronous I/O:

    1. synchronous I/O: When a process invokes an I/O operation function, the process is suspended (that is, blocked) until the I/O operation function returns, until the I/O operation completes;
    2. asynchronous I/O: When a process invokes an I/O operation function, the process is not suspended (that is, not blocked) until the I/O operation function returns, and notifies the process when the I/O operation is complete;

There are 5 types of I/O models in Unix systems: blocking I/O, non-blocking i/o;i/o multiplexing, signal-driven I/O, asynchronous I/O;

Typically an input operation requires two stages: (1) waiting for data to be ready, (2) copying data from the kernel to the process ;

Blocking I/O

When a process invokes an I/O operation, if the data is not ready, the process will remain blocked until the data is ready and copied from the kernel to the buffer of the application process. That is, both phases of the I/O operation are blocking states.

In the case of UDP datagram reading, if the process calls the Recvfrom function to read data from the socket, if the socket buffer does not have data at this point, the process blocks the Recvfrom function call until the socket buffer has data and reads the data into the process buffer and returns. Until the Recvfrom function returns, the process is stuck in a blocked state.



Non-blocking I/O

When the process invokes an I/O operation, if the data is not ready, a ewouldblock error is returned immediately until the data is ready and the application process polls to check if the data is ready before the data is ready. When the data is ready, the process is blocked until the data is copied before the kernel copies the data to the buffer of the application process. That is, the first phase of the I/O operation is in a polling state and the second phase is in a blocking state.

In the case of UDP datagram reading, if the process calls the Recvfrom function to read data from the socket, if the socket buffer does not have data at this point, a ewouldblock error is returned to the process, and the application process polls to check whether the data is ready before the data is ready. Returns until the socket buffer has data and the data is read to the process buffer.


I/O multiplexing

In I/O multiplexing, the general system calls the Select or poll function to block the system call of Select or poll before the data is ready, without blocking the I/O operation. When select or poll discovers that the data is ready, the data is copied from the kernel to the buffer of the application process through the actual I/O operation. That is, the first stage is blocking the system call on select or poll, and the second stage is blocking the actual I/O operation.

As an example of a UDP datagram reading process, if the process calls the SELECT function to wait for the datagram socket to become readable, when the Select function returns a socket readable, call recvfrom to copy the read datagram to the application process buffer.


Signal-driven I/O

When a process invokes an I/O operation, if the data is not ready, it waits for the data to be ready and does not block during the wait. If the data is ready at some point, the kernel notifies the process to initiate I/O operations and copies the data from the kernel to the application process buffer. That is, the first phase is in the wait phase, at which time the process does not block and the second phase blocks the actual I/O operation.

First, the signal-driven I/O function of the socket is turned on, and a signal handler function is called through the function Sigaction system, the system call returns immediately and the process continues to work, that is, the process is not blocked. When the datagram is ready to read, the kernel generates a SIGIO signal for the process, captures the signal and processes it, invokes the Recvfrom function in the handler to read the datagram, notifies the main loop that the data is ready to be processed, and notifies the main loop to read the datagram. Processes are not blocked while waiting for datagrams to arrive.


asynchronous I/O

The application process initiates an asynchronous I/O operation and notifies the application process that the kernel has completed the entire operation (copying data from the kernel to the buffer that should be the process). During the entire process, the process is not blocked.

Call the Aio_read function to pass a descriptor, buffer pointer, buffer size, and file offset to the kernel, and tell the kernel how to notify the process when the entire operation is complete. The system call returns immediately, and the process is not blocked while waiting for I/O to complete.



Comparison of 5 I/O models

The front four types of blocking I/O, non-blocking I/O multiplexing, and signal-driven I/O are synchronous I/O. Shows the differences between the 5 I/O models.





Resources:

"Unix Network Programming"

"Network Programming" I/O model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.