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

How to analyze five programming models in programming language

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces how to analyze the five programming models in programming language. the content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Programming model, we can simply understand that it is a template, encountered similar problems can be easily solved according to the template, which simplifies the programming problem. Different programming environments and different application objects have different programming models. Programming model is also the basic knowledge of learning programming content, Xiaobian takes you to analyze the five programming models.

Synchronous, asynchronous, blocking and non-blocking differential relations

In fact, synchronization and asynchronism are for the interaction between the application and the kernel. During synchronization, the process triggers the IO operation and waits (what we call blocking) or polls to see if the IO operation (that is, non-blocking) is complete. After the process triggers the IO operation in the asynchronous process, it returns directly and does its own thing. The IO is handed over to the kernel to deal with. After completion, the kernel informs the process that the IO is complete. For applications, synchronous and asynchronous focus on the collaborative relationship between programs; blocking and non-blocking are more concerned with the execution status of a single process. Synchronization can be divided into blocking and non-blocking, asynchronous there is no, it must be non-blocking. Blocking, non-blocking, and multiplex IO multiplexing are all synchronous IO, and asynchronous must be non-blocking, so there is no such thing as asynchronous blocking and asynchronous non-blocking. True asynchronous IO requires the deep involvement of CPU. In other words, only when the user thread operates the IO does not consider that all the execution of the IO is left to the CPU to complete, and only waits for a completion signal, it is the real asynchronous IO. So, pulling a child thread to poll, to loop, or to use select, poll, or epool is not asynchronous.

Synchronization: after performing an operation, the process triggers the IO operation and waits (what we call blocking) or polls to see if the IO operation (that is, non-blocking) completes, waits for the result, and then continues with the subsequent operation.

Async: after performing one operation, you can perform other operations, and then wait for the notification to come back to perform the operation that you just didn't finish.

Blocking: after the process communicates a task to the CPU, it waits for the CPU processing to complete before performing the next operation.

Non-blocking: after the process communicates to the CPU, it continues to process the subsequent operation, and then asks if the previous operation is completed in the interval time. In fact, this process is also called polling.

2. IO model (five programming models)

Here, the system call recv under Linux is used as an example, which is used to receive a message from the socket. Because it is a system call, the call will be switched from the user process space to the kernel space for a period of time and then switched back. By default, recv waits until the network data arrives and is copied to the user's process space or when an error occurs, and the fourth parameter, flags, allows it to return immediately.

1. Blocking IO model

Use the default parameter of recv to wait for the data until it is copied to user space, during which time the process is always blocked. A fill the cup with water, turn on the faucet to fill it and leave. This process can be seen as using the blocking IO model, because if the faucet does not have water, he will have to wait until the water is available and fill the cup before he can leave to do something else. Obviously, this IO model is synchronous.

2. Non-blocking IO model

Change the flags to let recv return regardless of whether the data is obtained or not, and then call recv after a period of time if there is no data. This is a loop. Classmate B also filled the cup with water. After turning on the faucet, he found that there was no water. It left. After a while, he took the cup and came to have a look. In the middle of leaving these times, classmate B left the water loading site (back to the user process space) and could do his own thing. This is the non-blocking IO model. But it only checks that it is non-blocking when there is no data, and still has to wait for the data to be copied to user space when the data arrives (waiting for the water to fill the cup), so it is still synchronized IO.

3. IO reuse model

Here, call select or poll before calling recv. Both system calls can inform the user process when the kernel prepares data (network data arrives at the kernel). There must be data to call recv at this time. So in this process, it blocks select or poll, but not recv. Some people define non-blocking IO as IO operations that do not block system calls during read and write operations (excluding blocking when data is copied from the kernel to user space, because this is really short compared to network IO). If you understand it this way, this IO model can also be called a non-blocking IO model, but according to POSIX, it is also synchronous IO. Then it is also called synchronous non-blocking IO as it is upstairs.

This IO model is special, divided into segments. Because it can monitor multiple file descriptors (fd) at the same time. At this time, classmate C came to fill the water and found that there was a row of faucets. The aunt told him that none of these faucets had any water, and so tell him when there was water. So wait and wait (select call), after a while his aunt told him there is water, but do not know which faucet has water, see for yourself. So students C opened it one by one and filled the cup with water (recv). By the way, the famous epoll (synonym for high performance), epoll also belongs to the IO reuse model, the main difference is that the housekeeper will tell C which faucets have water, do not need to open one by one to see (of course, there are other differences).

4. Signal-driven IO model

By calling the sigaction registration signal function, the system interrupts the current program when the kernel data is ready, and executes the signal function (where recv is called). Classmate D asked his aunt to inform him when there was water (register signal function). It wasn't long before classmate D learned that there was water and ran to load it. Is it very much like asynchronous IO? Unfortunately, it still synchronizes the IO (it doesn't save the time of loading water).

5. Asynchronous IO model

Call aio_read, get the kernel and other data ready, and copy it to the user process space and execute the pre-specified function. Classmate E asked the housekeeper to inform him after filling the cup with water. The whole process E students can do other things (no recv), this is the real asynchronous IO.

Generally speaking: blocking IO model, non-blocking IO model, IO multiplexing model (select/poll/epoll), and signal-driven IO model are all synchronous IO, because phase 2 is blocking (albeit for a short time). Only the asynchronous IO model conforms to the meaning of POSIX asynchronous IO operations, and everything else can be done in either phase 1 or phase 2.

On how to analyze the five programming models in the programming language is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Development

Wechat

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

12
Report