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

What are the five models of IO

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what are the five models of IO". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

We often mention such nouns as IO and NIO. So, what exactly is IO? What is NIO?

In addition, we usually hear two very similar sets of concepts: blocking / non-blocking and synchronous / asynchronous. So what's the difference between blocking and non-blocking? What's the difference between synchronous and asynchronous? Many students are easy to confuse these two groups of concepts and can't explain them clearly.

Therefore, this tutorial starts from the perspective of network IO and uses common cases in life to speak the five models of IO, as well as the above two sets of concepts.

User space and kernel space

The core of the operating system is the kernel, which is independent of ordinary applications, can access the protected kernel space, and has all the permissions to access the underlying hardware devices. In order to protect the security of the kernel, the operating system generally forces user processes not to operate the kernel directly, so the operating system divides the memory space into two parts: kernel space and user space.

It's like, the restaurant owner divides the whole hotel into two parts: the hall and the kitchen. The hall is used for customers to eat, the kitchen is used for chefs to cook, and the kitchen door is usually written: "Kitchen is important, no entry for idle people", that is, customers generally do not have the characteristics of using the kitchen directly.

Therefore, when we use TCP to send data, we need to copy the data from the user space to the kernel space first, and then send the data from the kernel space by the kernel operation; when we use TCP to read the data, the data is first prepared in the kernel space, and then copied from the kernel space to the user space for user processes to use.

This is like, when we eat in a restaurant, we order food in the living room first, and then the waiter passes our menu into the kitchen; when the kitchen is ready, it is passed from the waiter to the living room.

Therefore, an IO read operation is divided into two phases (write operations are similar):

Wait for the kernel space data preparation phase

Data is copied from kernel space to user space

To this end, Unix divides IO into the following five IO models according to these two stages:

Blocking IO

Non-blocking IO

IO multiplexing

Signal driven IO

Asynchronous IO

Let's do it one by one.

Blocking IO

Blocking IO, that is, when a user process initiates a request, it blocks until the data is copied into user space and then returns.

The blocking IO is continuously blocked in two phases until the data is returned.

This is like, you go to the roadside to buy fast food, this shop is relatively low-level, only one car and one owner. After ordering, you foolishly watch the boss start to order food, and then bring it to you. In the whole process, you can only watch the boss finish the food and bring it to you. You are blocked at both stages.

Non-blocking IO

Non-blocking IO, the user process keeps asking the kernel, is the data ready? Try again until the kernel says the data is ready, then copy the data from kernel space to user space, return successfully, and start processing the data.

Non-blocking IO does not block in the first stage and blocks in the second stage.

It's like going to a small stir-fry shop, which is a bit more advanced and has an independent storefront. After ordering, you can wait while playing with your mobile phone. After a while, you run over and ask the boss, "is my food ready?" the boss says, "not yet." after a while, you run over and ask "is my food ready", and the boss says "not yet". After a few times, you said, "Boss, is my food ready?" the boss said, "here comes", and then you watch him bring the food to you. In the whole process, you don't have to block when you ask "is the food ready?" the boss immediately responds you. You can play with your phone immediately, but when you serve food, you look at it foolishly. During this period, you can't play with your phone. You are blocked.

IO multiplexing

IO multiplexing, multiple IO operations use a selector (selector) to ask which IO is ready, selector is responsible for notifying those IO whose data is ready, and then they request the kernel data themselves.

IO multiplexing, the first phase will block on the selector, the second stage of copying data will also block.

It's like going to a Sichuan restaurant. It's a big restaurant with a lot of people and a beautiful waiter. After you ordered, you hooked up with the waiter, "Beauty, I'll order a spicy chicken, let me know when it's ready." the beauty didn't talk to you. That's how other people hook up with beautiful women. Then, the beautiful woman was so busy that she went to the kitchen every once in a while to see which dishes were ready, and every time she came out, she would shout, "then who, your food is ready, come and serve it yourself." . During the whole process, it is blocked for a beautiful woman to go to the kitchen to watch the food, because she has to wait for a while when the food is not good; it is also blocked when you run to serve the food. Part of the block on the beauty, part of the block on you.

Signal driven IO

Signal-driven IO, the user process registers a signal to the kernel to indicate what data it needs before initiating the read request. The registration request returns immediately. When the kernel data is ready, the user process actively notifies the user process, and then requests to read the data. At this time, the user process needs to wait for the data to be copied from the kernel space to the user space before returning.

Signal driven, no blocking in the first stage and blocking in the second stage.

It's like going to McDonald's to eat McDonald's. After you order a meal on the machine next to you, there is a small ticket "number 1024", and then you wait while playing with your cell phone. After a while, the horn shouted, "1024, please pick up your meal. 1024, please pick up your meal." Then you ran to pick up your meal. Throughout the process, the order is returned immediately, and then do whatever you want without blocking (that is, you don't have to wait for the meal to be ready); in the process of picking up, you need to be served from the counter to your position, which is blocked.

Asynchronous IO

Asynchronous IO, the user process immediately returns after initiating a read request, and notifies the user to use the data directly when the data is completely copied to the user space.

Asynchronous IO, with no blocking in both phases.

This is like, you eat "fish powder". After scanning the code to order, you don't have to worry about it at all. after a while, an aunt brought the food to you and said kindly, "take your time, sir," and then you happily ate this bowl of "golden soup and fish powder". In the whole process, you don't have to wait for the fish powder to be ready, nor do you have to watch the aunt bring the food to you or yourself, which is completely non-blocking and purely asynchronous. Therefore, this experience is the best.

So, if you divide the eating process into two parts: "preparing the meal" and "serving the dish", then:

If you wait for the two phases to complete, you are blocking the IO.

If you ask "is the food ready" every once in a while, you can play with your mobile phone, but when you serve the food, you foolishly watch the boss bring it over, which is a non-blocking IO.

If you and others entrust the waiter to take a look at "is the food ready" for you every once in a while, but you need to serve the dish yourself, that is, IO multiplexing.

If the machine orders and the machine calls to pick up the meal, it is the signal-driven IO.

If the order is scanned and served automatically, it is asynchronous IO.

Blocking and non-blocking

Blocking means that the current thread is suspended until the call result is returned. For example, if you wait for the end of the dish, you are blocked.

Non-blocking means that the current thread is not suspended but can continue to do something else until the result is not immediately available. For example, if you play with your phone and wait for the meal to be ready, you are non-blocking.

To put it simply, blocking calls you have to hang up and wait for the results to return, non-blocking calls you don't care about the results, what do you like to do after the call.

Synchronous and asynchronous

For synchronization and asynchronism, let's look directly at the definition in POSIX:

A synchronous I/O operation causes the requesting process to be blocked until that I/O operation completes

An asynchronous I/O operation does not cause the requesting process to be blocked

In synchronization, the caller is blocked until the IO operation is complete, and the result of the call is returned with the end of the request.

Asynchronously, the caller is not blocked, and the result of the call is not returned with the end of the request, but in the form of a notification or callback function.

Blocking / non-blocking, more concerned about whether the current thread is suspended.

Synchronous / asynchronous, more concerned about whether the result of the call is returned with the end of the request.

Blocking here refers to whether there is blocking throughout the IO process, or more specifically, whether the system call recvfrom will block. In our case, it can be understood as whether the behavior of "serving the dish" is blocking for you.

Therefore, blocking IO, non-blocking, IO multiplexing and signal-driven IO are all synchronous IO, and only the last one is asynchronous IO.

Why not choose asynchronous IO?

From the above analysis, asynchronous IO is the best IO model, so why don't we choose asynchronous IO?

That's because asynchronous IO is not mature on linux, and our servers are usually linux, so most frameworks don't support asynchronous IO very much now, including Netty, which has been implemented before, but has been abandoned later.

That's all for "what are the five models of IO"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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