Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Five kinds of common Istroke O models in network programming

2025-02-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Ipaw O model

There are five Istroke O models under Unix:

1 >: blocking Istroke O

2 >: non-blocking Istroke O

3 >: Icano Multiplexing

4 >: signal-driven Istroke O

5 >: asynchronous Ithumb O

Among them, the first four are synchronous Ithumb O model, and only the fifth is asynchronous.

Synchronous and asynchronous:

The concept of synchronization here is different from that in communication between two entities. Synchronization here means that a series of actions in this Ibind O need to be completed by yourself, whether you are waiting for the event to occur (blocking) or when an event is ready for you to complete the following actions (non-blocking) belong to synchronization.

Asynchronous, it means to call another executor to complete, when the executor finds the time to deal with you, you finish this thing, the process of execution has nothing to do with your action.

Blocking and non-blocking:

Blocking means waiting for an event to happen, and if it doesn't happen, wait until the event occurs.

Non-blocking, do not need to wait, when it has to return to deal with.

1 > blocking Imax O

The Icano function is used, which causes the program to block and wait for data, and keep waiting if the data is not ready.

For example, when we use the blocking socket to create a socket, we call accept/recvfrom/connect... Waiting will be blocked waiting for a connection, request or data to arrive.

Blocking function is very simple to implement, but it has many problems. First of all, as a network server, blocking waiting will waste a lot of resources and time. When the server calls the function sendto to send data to a remote client, it is obviously inappropriate to do nothing else. The solution to this problem is often to create a process or thread to solve this problem, because the cost of the process is far greater than that of the thread, so it is feasible to use a multi-threaded approach.

The advantage of blocking IWeiO is that it is very stable, it can guarantee the connection to receive and send data certainty, which is the reason why most large Internet companies use multithreading, and the use of thread pool will greatly increase the efficiency of thread opening.

2 > non-blocking IBG O

Call the socket function and set it to non-blocking mode, set the mode, and call the fcntl () function under linux.

In non-blocking mode, the current execution flow no longer waits for requests or data to arrive in sleep, but uses polling. The operation flow of this method is like this: it returns every other period of time. If any data arrives, it returns. If not, an error code WSAEWOULDBLOCK is returned. We need to constantly use a loop to wait for the successful return of the data, which often takes up a lot of CPU.

The disadvantage of non-blocking, although it no longer has to make waiting too stiff, it inevitably can only wait for one event at a time.

3 > signal-driven Ithumb O

The following events in TCPsocket will produce SIGIO signals, because there are too many reasons for this signal at the same time, we can not distinguish which situation generated the SIGIO signal, so the signal driver is not suitable for use in TCP, on the contrary, it is more suitable to be used in UDP.

The signal-driven operation is shown in the following figure

4 > I _ Band O multiplexing.

The way it works in this way is quite special. According to TCPsocket, for example, he will put it in a collection when listen has a new link. When the socket in this collection is read, we will read it, and when there is writing, we will write it. It is non-blocking I _ pico and signal-driven I _ hand O plus can wait for more than one. The combination of these three, the common functions are select,poll,epoll and so on. These functions will be described in detail in a later blog.

Its disadvantage is that no matter which function described above, each time a data is sent or received, it goes into two kernel-to-user conversions.

The model of Ipaw O multiplexing is shown below.

5 > Asynchronous Imax O

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 and output operation through status, notification, and callback after completion.

The comparison of the five Icano models is as follows

It can be seen that the best way is asynchronous Iripple O. Of course, it is more responsible to implement, and the reliability of blocking Iripple O is the best.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report