In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
The main content of this article is to explain "what is the concept of Linux's Icano model". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the concept of Linux's Imax O model"!
1. Blocking Ipaw O model
Lao Li went to the railway station to buy a ticket and stood in line for three days to get a refund.
Cost: eat, drink and sleep at the station for 3 days, nothing else.
two。 Non-blocking Ipaw O model
Lao Li went to the railway station to buy a ticket, went to the railway station every 12 hours to ask if there was a refund, and got a ticket three days later.
Cost: 6 times to and from the station, 6 hours on the road, other time to do a lot of things.
3. I use O reuse model
1.select/poll
Lao Li went to the railway station to buy tickets, commissioned scalpers, and then telephoned scalpers every 6 hours. Scalpers bought tickets within three days, and then Lao Li went to the railway station to pay for the tickets.
Cost: 2 times to and from the station, 2 hours on the road, 100 yuan for scalpers, 17 phone calls
2.epoll
Lao Li went to the railway station to buy a ticket, commissioned scalpers, and immediately informed Lao Li to get it, and then Lao Li went to the railway station to pay for the ticket.
Cost: 2 times to and from the station, 2 hours on the road, scalpers charge 100 yuan, no need to make a phone call
4. Signal-driven Icano model
Lao Li went to the railway station to buy a ticket and left his phone number for the conductor. After the ticket was available, the conductor telephoned Lao Li, and then Lao Li went to the railway station to pay for the ticket.
Cost: 2 times to and from the station, 2 hours on the road, free scalpers 100 yuan, no need to make phone calls
5. Asynchronous Ipaw O model
Lao Li went to the railway station to buy a ticket and left his phone number for the conductor. After the ticket was available, the conductor telephoned Lao Li and delivered the ticket to his door.
Cost: one round trip to and from the station, one hour on the road, 100 yuan free of scalpers, no need to make phone calls
Summary:
The difference between 1 and 2 is that you poll yourself.
The difference between 2 and 3 is that yellow cattle are commissioned.
The difference between 3 and 4 is that the telephone takes the place of scalpers.
The difference between 4 and 5 is whether the telephone notice is to pick up or deliver the ticket to the door.
2. Explanation
For more information on how nginx uses epoll to receive requests, take a look at the above figure. As a reminder, ngnix will have a lot of links, and epoll will monitor them all, and then, like turning on the switch, dial whoever has the data, and then call the corresponding code for processing.
Horizontal axis Dead connections means the number of links, but its testing tool is called deadcon. The vertical axis is the number of requests processed per second, and you can see that the number of requests processed by epoll does not decline as the number of links increases. Poll and / dev/poll are miserable. But epoll has a fatal flaw. Only linux supports it.
I can see some explanations above in Zhihu as follows:
The screenshot is as follows:
3. Summary
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.
File IZP O
Files are read and written in different ways, and there are also many I-O classifications of documents, including
Buffered and unbuffered Istroke O
Direct and indirect Istroke O
Blocking and non-blocking I _ VS synchronous and asynchronous I _ jump O
Next, these classifications are discussed respectively.
Buffered and unbuffered Istroke O
The standard library for file operations can cache data, so according to "whether to use standard library buffering or not", the file Imax O can be divided into buffered Imax O and unbuffered Imax O:
Buffering Ihop O uses the cache of the standard library to accelerate the access of files, while the standard library accesses files through system calls.
Unbuffered IPUBO accesses files directly through system calls without going through the standard library cache.
The "buffering" here refers specifically to the buffering implemented within the standard library.
For example, many programs only output when they encounter line breaks, and the content before line breaks is temporarily cached by the standard library. The purpose of this is to reduce the number of system calls. After all, system calls have the overhead of CPU context switching.
Direct and indirect Istroke O
We all know that disk IMaGO is very slow, so the Linux kernel will copy user data into the kernel and cache it after the system call in order to reduce the number of disk IAccord O. This kernel cache space is "page cache". Only when the cache meets certain conditions will the request for disk IMab O be made.
Then, according to whether or not to make use of the operating system's cache, the file Imax O can be divided into direct Imax O and indirect Imax O:
If you directly use IZP O, you will not copy the data between the kernel cache and the user program, but access the disk directly through the file system.
During the read operation, the data is copied to the user program from the kernel cache, and during the write operation, the data is copied from the user program to the kernel cache, and then the kernel decides when to write the data to disk.
If you specify the O_DIRECT flag when using the system call function of the file manipulation class, you use the direct Icano. If it is not set, the default is indirect Imax O.
Under what circumstances will the kernel write cached data to disk if indirect Iramp O is used for write operations?
The following scenarios trigger kernel cached data to be written to disk:
At the end of the call to write, when it is found that the kernel caches too much data, the kernel will write the data to disk.
If the user actively calls sync, the kernel cache will be brushed to the disk.
When memory is so tight that pages can no longer be allocated, the kernel cached data will also be brushed to disk.
When the cache time of the kernel cached data exceeds a certain time, the data will also be brushed to disk.
Blocking and non-blocking I _ VS synchronous and asynchronous I _ jump O
Why put blocking / non-blocking and synchronous and asynchronous together? Because they are indeed very similar and very confusing, but the relationship between them is a bit delicate.
Let's take a look at blocking read O first. When the user program executes the read, the thread will be blocked until the kernel data is ready and copied from the kernel buffer to the application buffer. When the copy process is complete, the thread will return.
Note that blocking waits for the two processes of "kernel data ready" and "data copied from kernel state to user state". The process is shown below:
Non-blocking IPUBO
Note that the last read call here, the process of getting data, is a synchronous process, a process that needs to be waited. Synchronization here refers to the process of copying kernel-state data to the cache area of the user program.
For example, when accessing a pipe or socket, if the O_NONBLOCK flag is set, it means that the access is in a non-blocking way, and if nothing is set, it is blocked by default.
It feels a bit silly every time the application polls the kernel for whether it is ready, because in the process of polling, the application does nothing but cycle.
In order to solve this silly polling mode, Imax O multiplexing technology has come out, such as select, poll, which is distributed through IWeiO event, and then notifies the application to operate with event when the kernel data is ready.
This greatly improves the utilization of CPU by the application process, and the application process can use CPU to do other things without being notified.
The following figure shows the process of multiplexing using select Iram O. Note that the process of obtaining data by read (the process of copying data from kernel state to user state) is also a synchronous process, which requires waiting:
Asynchronous IPUBO
The following figure summarizes the above Imax O models:
As we know earlier, Istroke O is divided into two processes:
The process of data preparation
The process of copying data from kernel space to the user process buffer
Blocking Iripple O will block in "procedure 1" and "procedure 2", while non-blocking IAccord O and the multiplexing based on non-blocking IAccord O will only block in "procedure 2", so all three can be considered synchronous IAccord O.
Unlike asynchronous Iripple O, neither procedure 1 nor procedure 2 will block.
Use stories to understand these Igamot O models
For example, if you go to the canteen to eat, you are like a user program and the canteen is like an operating system.
Blocking Icano is like going to the canteen to eat, but the food in the canteen is not ready yet, and then you have been waiting there for a long time until the food is served out by the aunt in the canteen (the process of data preparation), but you still have to wait for your aunt to type the food (kernel space) into your lunch box (user space) before you can leave.
For example, when you go to the canteen and ask your aunt whether the food is ready or not, you leave. Dozens of minutes later, you come to the canteen to ask your aunt that it is done, so your aunt helps you hit the food into your lunch box, and you have to wait for this process.
For example, when you go to the canteen and find a row of windows, the auntie tells you that these windows are not ready yet, and then let you know when they are ready, so wait (select call), after a while, the aunt informs you that the food is ready, but you don't know which window is ready, so you can see for yourself. So you can only confirm it one by one, and then you find that the food in window 5 is ready, so you ask the aunt in window 5 to help you pick up the food into the lunch box. You have to wait for the process, although it doesn't take long. After you finish the meal, you can naturally leave.
For example, you ask the auntie in the canteen to cook the food and put it into the lunch box, and then send the lunch box to you. You don't have to wait for the whole process.
At this point, I believe that you have a deeper understanding of "what is the concept of Linux's Icano model". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.