Difference between socket blocking and non-blocking)

Source: Internet
Author: User
Tags socket blocking

Simply put:

Blocking means you cannot finish it,
If you are not in a group competition, you should do it first. I want to see if there is anything else. Let me know when it's over.

Let's take the most commonly used send and recv functions for example...
For example, if you call the send function to send a certain Byte, the work done by sending in the system is actually to transfer data (Copy) to the output buffer of the TCP/IP protocol stack, it is successfully executed and
It does not mean that the data has been successfully sent out. If the TCP/IP protocol stack does not have enough buffer to store the data you copied... at this time, the blocking and non-blocking are different.
Point: For socket in blocking mode
The send function will not return until the system buffer has enough space to Copy the data you want to send before returning it. For non-blocking socket, The send function will return immediately.
WSAEWOULDDBLOCK tells the Caller: "The sending operation is blocked !!! You want to solve it ..."
For the recv function, the internal working mechanism of the function is actually waiting for the receiving buffer of the TCP/IP protocol stack to notify it: Hi, your data is coming. For the socket in the blocking mode
For example, if the receiving buffer of the TCP/IP protocol stack does not notify a result to the receiver, it will never return: system resources are consumed .... for non-blocking socket, this function will return immediately.
I will tell you: WSAEWOULDDBLOCK --- "there is no data now. Let's look back"

 

Extension:

During network programming, we often see four call Methods: synchronous, asynchronous, blocking, and non-blocking. These methods are not easy to understand. The following is my understanding of these terms.
Synchronization
The so-called synchronization means that when a function call is sent, the call will not return until the result is not obtained. According to this definition, most functions are called synchronously (for example, sin,
Isdigit ). But in general, when we talk about synchronization and Asynchronization, we are referring to the tasks that require the collaboration of other components or a certain amount of time. The most common example is
SendMessage. This function sends a message to a window. This function does not return a message before the recipient finishes processing the message. After the other party completes processing, this function returns the message processing function.
The LRESULT value is returned to the caller.
Asynchronous

Asynchronous concept and synchronization relative. When an asynchronous process is called, the caller cannot obtain the result immediately. After the call is completed, the caller is notified by status, notification, and callback. To
The CAsycSocket class is used as an example (note that CSocket is derived from CAsyncSocket, but the function has been converted from Asynchronous to synchronous). When a client calls
After the Connect function sends a connection request, the caller's thread can immediately run down. After the connection is established, the socket underlying layer sends a message to notify the object. Run
The component and caller return results in three ways: Status, notification, and callback. Which one can be used depends on the implementation of the execution part, unless the execution part provides multiple options, it is not controlled by the caller. If the Execution Department
When the status is used for notification, the caller needs to check each time and the efficiency is very low (some beginners who are new to multi-thread programming prefer to use a loop to check the value of a variable, this is actually a very serious
). If you use the notification method, the efficiency is very high, because the execution of components almost do not need to do additional operations. As for the callback function, there is actually no much difference with the notification.
Blocking

Blocking call means that the current thread will be suspended before the call result is returned. The function is returned only after the result is obtained. Some people may equate blocking calls with synchronous calls. In fact, they are different. For
For step-by-step invocation, the current thread is still activated, but the current function does not return logically. For example, we call the Receive function in CSocket.
Data, this function will wait until data is returned. At this time, the current thread will continue to process a variety of messages. If the main window and the called function are in the same thread, unless you operate on a special interface
Function call. In fact, the main interface should be refreshed. Another function recv used by socket to receive data is an example of blocking calls. When the socket works in blocking mode,
If this function is called without data, the current thread will be suspended until data exists.
Non-blocking
The concept of non-blocking corresponds to blocking, which means that the function will not block the current thread and return immediately before the result cannot be obtained immediately.
Object blocking mode and function calling
Whether the object is in blocking mode is highly correlated with whether the function is blocked or not, but it is not one-to-one. Blocking objects can have non-blocking calling methods. We can use certain APIs to poll objects.
To avoid blocking. For non-blocking objects, calling special functions can also be blocked. The select function is an example.

Blocking Communication

--------------------------------------------------------------------------------

Through overlapping communication and computing, high performance can be improved in many systems. An intelligent communication controller automatically executes communications. Light-the clue is to get this
An overlapping mechanism. Leading to good performance
An optional mechanism is to use non-blocking communication. A blocked sending starts to call the initialization of this sending operation, but it cannot be completed. Before the message is copied from the sending cache, the call to start sending will be returned.
An independent "sending completed" call is required to complete the communication, for example, to check the data copied from the sending cache. Use the appropriate hardware to convert the data stored by the sender after the sender is initialized and before it is completed
It can be performed simultaneously with the computation completed by the sender. Similarly, a non-blocking "receive start call" initializes the receiving operation,
But cannot complete it. Before a message is stored in the receiving cache, this call will return. An independent "receive completed" call is required to complete the receiving operation and check the received data cached.
With appropriate hardware, after receiving operation initialization and before it completes, the data stored in the receiver can be converted at the same time as the computing. The use of non-blocking receiving means that information is provided earlier in the receiving cache location
To avoid system caching and copying from memory to memory.

Non-blocking sending start calling can use the same four modes as blocking sending: Standard, cache,
Synchronization and preparation modes. These have the same meaning. No matter whether a matched recipient is logged on or not, you can start sending in all modes except "prepared". If a matched recipient is logged on, you can start a non-
Blocking "ready" sending. In all cases, the start call of sending is local: no matter the status of other processes, it immediately returns. If this call causes some system resources to run out, it will fail and return
An error code. High-quality MPI implementation should ensure that this situation only occurs when it is "Diseased. That is, one MPI implementation will support a large number of pending non-blocking operations.

When the data has been copied from the sending cache, this sending completion call is returned. It can have additional meaning, depending on the sending mode.

If the sending mode is "synchronous", only one matching recipient can complete the sending. That is, a receiver has been logged in and is matched with the sender
Configuration. In this case, the call to the sending completion is non-local. Note: If a synchronous, non-blocking message matches a non-blocking message before the call is completed,
It can be done. (Once the sender "knows" the conversion will end, it will be able to complete, but before the receiver "knows" the conversion will end ).

If the sending mode is "cached" and the message is not suspended, the message must be cached. At this time, the sending completion call is local, and it must be successful regardless of the status in which a request is received.

If the sending mode is standard and the message is cached, the sending end call can be returned before a matching message is received. On the other hand, sending is complete until a matching message is received, and the message has been copied to the receiving cache.

Non-blocking sending can be matched by blocking receiving, or vice versa.

Suggestions for users. A sending operation can be delayed in standard mode, and must be delayed in the same mode until a matching user receives the login. In both cases, the use of non-blocking sending allows the sender to proceed ahead of the receiver, so that the calculation of the speed of the two processes is more tolerant of fluctuations.

Non-blocking sending in cache and readiness modes has a limited impact. One possible non-blocking message will be returned, and one blocking message will be returned after the data is copied from the sender's storage. As long as data copying can be the same as computing, the use of non-blocking transmission has advantages.

The message sending mode implies that communication is initiated by the sender. When the sender initializes communication (data is directly moved to the receiving cache,
Does not require queuing up a pending sending request)
If a receiver has logged on, this communication usually has a low extra burden. However, only after a matched message has been sent can one receive operation be completed. When non-blocking receiving waits for sending, it does not block receiving.
Can be used to lower the additional burden of communication. (End with your suggestions ).

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.