Linux Network Programming Learning (11)-----Five I/O modes (chapter sixth)

Source: Internet
Author: User
Tags readable

1. What are the five I/O modes?

Blocking I/O, non-blocking i/o,i/o multiplexing, signal-driven I/O (SIGIO), asynchronous I/O

In general, there are two steps for the input operation of the program, one is to wait for the data to be readable, and the other is to copy the data from the system kernel to the dataset area of the program.

For a socket data operation, the first step is to wait for the data to be uploaded from the network locally, when the packet arrives, the data will be copied from the network layer to the buffer of the kernel, the second step is to copy the data from the kernel to the data area of the program.

2. Introduction to five kinds of blocking modes

1) blocking I/O mode

When a socket is established, the default mode is blocking I/O mode, and for UDP the data-ready flag is simple: an entire datagram has been received or not received. For example, when a process calls the Recvfrom () function, and the data is ready, the process will wait for the data to be ready, the data to be copied to the program data area, and then return normally, so that the process calls Recvfrom () to the function to return this time is blocked.

2) non-blocking I/O

When a socket is set to non-blocking, it is tantamount to telling the kernel "when the I/O operation I requested cannot be completed immediately, you want my process to hibernate and wait, do not do so, please return an error to me immediately." is called Recvfrom () when the data is not ready, then the kernel immediately returns an error to the process, so non-blocking, need to loop constantly testing whether there is data readable, that is, polling, the application polling the kernel to check the I/O operation is ready, This would be a huge waste of CPU resources

3) I/O multiplexing

When I am using I/O multiplexing, call the Select () or poll () function, at which time the data is read with block mode calling select ()/poll (), that is, the data is not ready to wait until the data is ready to return, and then call the Recvfrom () function to copy the data to the program buffer. A function is called more than a blocking pattern, but the advantage of multiplexing is that select () can wait for multiple socket descriptors, and the Select () function can return as long as there is a ready.

The use of multiplexing technologies is:

    • When a client needs to handle input and output operations for multiple file descriptors at the same time (generally standard input and output and network sockets), I/O multiplexing technology will have the opportunity to be used

    • When a program needs to do multiple sockets at the same time
    • If a TCP server program handles both sockets that are listening for network connections and sockets that are already connected

    • If a server program uses both the TCP and UDP protocols
    • If a server uses multiple services at the same time and each service may use a different protocol (such as inetd)

4) Signal-driven I/O mode

Linux Network Programming Learning (11)-----Five I/O modes (chapter sixth)

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.