The understanding of blocking and non-blocking, synchronous and asynchronous, I/O model in network programming

Source: Internet
Author: User
Tags epoll error code sleep socket
I. Conceptual UnderstandingIn the network programming, we often see Synchronization (sync), asynchronous (async), blocking (block), non-blocking (Unblock) four ways to call:
Sync:
The so-called synchronization is that when a function call is made, the call does not return until the result is obtained. That is, one thing must be done, so that the next thing is done before you can do it.
Async:
Asynchronous concepts and synchronization are relative. When an asynchronous procedure call is made, the caller cannot get the result immediately. The part that actually handles the call notifies the caller via status, notification, and callback after completion.
Blocking:
A blocking call means that the current thread is suspended until the call results are returned (the thread goes into a non-executable state where the CPU does not allocate a time slice to the thread, that is, the thread pauses). Functions are returned only after the result is obtained.
One might equate blocking calls with synchronous invocations, in fact they are different. For synchronous calls, many times the current thread is still active, but logically the current function does not return. For example, we call the RECV function in the socket, and if there is no data in the buffer, the function waits until there is data to return. At this point, the current thread will continue to process a wide variety of messages.
non-blocking:

The concept of non-blocking and blocking corresponds to a function that does not block the current thread and returns immediately until the result is not immediately available. blocking mode and blocking function calls for processes (threads):

Whether the process (thread) is in blocking mode and the function is not blocking the caller process (thread) has a strong correlation, but not one by one corresponds. In the blocking mode of the process (thread) can have a non-blocking call mode, we can use a certain API to poll the state, when appropriate to call the blocking function, you can avoid blocking (in this case, the following non-blocking IO mode will be discussed). For non-blocking objects, calling special functions can also go into blocking calls, which is an example of a function select.

summarized as follows:(1) synchronization, isI callA feature that features no end before I death the results.
(2) asynchronous, isI callA feature that does not need to know the result of the feature, notify me when the feature has results (callback notification); "After the call, I don't care, I go to do my thing, have the result will automatically notify me (callback)"
(3) obstruction, isCall me(function), I (function) will not return until I have finished receiving data or have not obtained the result.
(4) Non-blocking, isCall me(function), I (function) returns immediately, notifies the caller via select
The difference between synchronous IO and asynchronous IO is that:the process is blocked when the data is copied.
The difference between blocking IO and non-blocking IO is that:whether the application's call returns immediately.
Note:
In the above summary, be sure to experience synchronous/asynchronousI call, and in blocking/non-blockingCall me, but don't. ForI call, refers to theif I (app process/thread)Invokes a feature that may or may not be synchronous or asynchronous, and if it is synchronous, theMe (App process/thread)I waited until the feature returned, and if it was asynchronous, I could return directly after the call (application process/thread) and not wait for the function to return, but I couldn't get the result of the asynchronous function. So you have to wait until the function is complete (data and ready) for data processing by other means (callback function);Call meIsrefers to the application process call me (most of me here is a function), and my implementation may be blocking mode, or non-blocking mode, if it is blocking mode, I will block the caller process (thread), that is, the caller process (thread) is now suspended, the caller process (thread) is not awakened until I return to resume execution. In the case of non-blocking mode, the caller process (thread) calls me and I return immediately, thus not blocking the caller's process (thread).

for a simple C/s (client/server) mode:
Synchronization: Submit request, wait for server processing, processing completed returns this period the client browser cannot do anything
Asynchronous: Requests are processed through event triggering server processing (which is still something the browser can do)
Blocking and non-blocking refers to whether the process needs to wait when the data accessed by the process is not ready, simply saying that this is equivalent to the implementation difference within the function, that is, when it is not ready to return directly or wait for the ready;

While synchronous and asynchronous refers to the mechanism of accessing data, synchronization generally refers to the active request and wait for the completion of the I/O operation, when the data is ready to read and write must block (difference between ready and read and write two stages, synchronous read and write must block), asynchronous refers to the active request data can continue processing other tasks, and then wait for I/ Notification that the operation is complete, which allows the process to read and write data without blocking. (Wait for "notification")


Ii. Five types of I/O models under Linux Blocking I/O (blocking I/O), nonblocking I/O (nonblocking I/O), I/O multiplexing (SELECT and poll) (I/O multiplexing), signal-driven I/O (signal driven I/O (SI GIO), asynchronous I/O (asynchronous I/O (the POSIX aio_functions)). The first four are synchronous, and only the last is asynchronous IO.


(1) Blocking I/O model

The process blocks until the data copy completes the application calls an IO function, causing the application to block and wait for the data to be ready. If the data is not ready, wait .... The data is ready to be copied from the kernel to the user space, and the IO function returns a success indicator. Blocking I/O model diagrams: When calling the Recv ()/recvfrom () function, the process of waiting for data and replicating data in the kernel occurs.


When the recv () function is called, the system first checks to see if there is ready data. If the data is not ready, then the system is in a wait state. When the data is ready, the data is copied from the system buffer to the user space, and then the function returns. In a socket application, when the recv () function is called, the data is not necessarily present in the user space, then the recv () function is in a wait state.
Not all Windows Sockets APIs will block if the socket is called as a parameter call. For example, when you call the bind (), listen () function in a socket with blocking mode, the function returns immediately.
Using the blocking mode socket, the development of the network program is relatively simple and easy to implement. When you want to be able to send and receive data immediately, and the number of sockets processed is relatively small, it is appropriate to use blocking mode to develop a network program.

The inadequacy of blocking mode sockets is that it is difficult to communicate between a large number of well-established socket threads. When developing a network program using the producer-consumer model, each socket is assigned a separate read thread, a processing data thread, and an event for synchronization, which undoubtedly increases the overhead of the system. The biggest drawback is that when you want to handle a large number of sockets at the same time, it will not do, its extensibility is poor



(2) non-blocking IO model

The non-blocking IO calls the IO function repeatedly through the process (multiple system calls and returns immediately); In the process of copying data, the process is blocked; we set a socket interface to non-blocking to tell the kernel that when the requested I/O operation cannot be completed, do not sleep the process, but return an error. This way our I/O operations function will constantly test whether the data is ready, and if not, continue testing until the data is ready. In this continuous testing process, the CPU will be a lot of time.
Set the socket to non-blocking mode, that is, notify the system kernel: when calling the Windows Sockets API, do not let the thread sleep, but should let the function return immediately. On return, the function returns an error code. As shown in the figure, a non-blocking pattern socket calls the recv () function multiple times. The kernel data is not ready when you call the recv () function the first three times. Therefore, the function immediately returns the WSAEWOULDBLOCK error code. The fourth time the recv () function is called, the data is ready to be copied into the application's buffer, and the recv () function returns a successful indication that the application is starting to process the data.




(3) IO multiplexing model
mainly select and Epoll; for an IO port, two calls, two returns, has no advantage over blocking IO, the key is to enable simultaneous monitoring of multiple IO ports;
The I/O multiplexing model uses the Select, poll, Epoll functions, which also block the process, but unlike blocking I/O, these two functions can block multiple I/O operations at the same time. I/O functions can be detected at the same time for multiple read operations, multiple write operations, and I/O operation functions are not actually invoked until there is data readable or writable.


(4) signal-driven IO

Two calls, two returns; first, we allow the socket interface to drive the signal-driven I/O and install a signal processing function, and the process continues to run without blocking. When the data is ready, the process receives a sigio signal that can be called by the I/O operation function in the signal processing function to process the data.


(5) Asynchronous IO Model
The process does not need to block when copying data. When an asynchronous procedure call is made, the caller cannot get the result immediately. The part that actually handles the call notifies the caller of the input-output operation by state, notification, and callback after completion



Comparison of 5 I/O Models:


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.