In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly talks about "how to understand the Linux's Icano mechanism". 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 "how to understand the Linux Imax O mechanism".
Do you really know anything about multithreading?
If you were asked, "Why can multithreading improve the efficiency of programs?", you would probably say, "computers execute tasks in parallel, of course they are efficient!" This is obviously not the answer for an expert. You know, a CPU can only do one thing at any point in time. If you execute instruction A, you can't execute instruction B. how can it be parallel? If you want to really understand multithreading, you must first understand the Icano mechanism in Linux.
What is Linux I do O?
Generally refers to file read / write, network card read / write. The speed of preparing data is far lower than that of CPU processing data, whether it is the file ID O or the network card ID O. Disk read data, millisecond level, the slowest. The second is the network card, microsecond. Then there is memory, nanosecond, and the fastest is cache.
The following figure shows the execution of a Linux process (with only one main thread):
The user process initiates the read request to the CPU, the CPU initiates the Imax O request to the DMA, and the DMA initiates the Icano request to the disk. The full name of DMA here is Direct Memory Access, which you can simply understand as an agent between CPU memory and disk.
The disk initiates an I stop O interrupt to CPU, and CPU copies the data, first in kernel mode buffer, then to user mode buffer.
The user process switches back to the user state from the kernel state, the blocking state ends, and the program continues to execute until the end.
In the above process, the user process can only wait in place before getting the disk data, that is, it is in a blocked state, and the CPU is idle at this time, which obviously results in a waste of CPU resources. At this time, if you pull another thread T in the process, then T can make use of all the available space to make use of the idle resources of CPU while the main thread is waiting for data. This is the origin of multithreading technology.
There are always some concepts that make people dizzy-synchronous / asynchronous, blocking / non-blocking
In the process of learning multithreading technology, there are always some concepts that pop up from time to time, such as synchronous / asynchronous, blocking / non-blocking, if you are an expert in the field of concurrency, then these are pediatrics. If you are a beginner, you are likely to get caught up in it, unable to extricate yourself, and finally give up. Next, we try to end the problem and take you out of the hole.
For example:
If one day you want to go to the bank to handle a certain business, when you get to the bank, you must first get a number with your ID card. After taking the number, you find that there are many people on this day, so you have to wait and wait for the number to be called. While waiting, you have the following options:
Option 1: do nothing and stare at the bank's LCD screen. When your number appears, you get up and go straight to the counter. The bank's dialing system hasn't even had a chance to call your number. So, during this time, you are in a state of blocking, and your relationship with the bank dialing system is synchronized. "blocking" is easy to understand, because you haven't done anything! how do you understand "synchronization"? Because you have to keep pace with the bank's calling system, when your number appears on the screen, you have to go to the counter to do business immediately, and the bank's automatic broadcast calling your function is not necessary for you.
Option 2: you still do nothing, but you sit in your seat and start meditating and daydreaming. Because you know, when it's your turn, the bank dialing system will broadcast for you. After hearing the broadcast, it's not too late for you to get up and go to the counter. So during this period of time, you are still in a blocking state. And the relationship between you and the bank calling system is asynchronous, because you no longer have to pay attention to the number scrolling on the screen, it will naturally broadcast to you when it is your turn.
Option 3: for a smart person, he obviously won't sit there and wait or think about it. At this time, you pick up your phone and decide to play Arena of Valor. In the process of playing the game, you still glance at the display screen from time to time. When you see your number, you immediately get up and go to the counter. Then during this period of time, you will be in a non-blocking state, because you have done other things. But the relationship between you and the bank's calling system is still in sync.
Option 4: if you plan to concentrate on playing Arena of Valor, you don't want to cheat your teammates. Then you can just wait for the broadcast to call you. Then during this time, you are in a non-blocking state, and the relationship with the bank calling system is asynchronous.
All right, let's sum up:
Blocking means that when the requester initiates a function call to read data, when the data is not ready, if the function has been waiting for the return result, it is blocking; on the contrary, if the function returns immediately, continue to perform the following action is non-blocking.
In the Icano model, if the requester needs to participate in the process from the initiation of the request to the final preparation of the data, then this kind of request is called synchronous request; on the contrary, if the application no longer participates in the process after sending instructions and only needs to wait for the notification of the final completion result, then it is asynchronous.
Oh, right. You can also think of the above option 2 as option 4, because you can just wait for the bank broadcast to call you, and daydreaming can also be considered as doing your own thing!
Talk about multithreading again
The so-called multithreading means that multiple threads run in the same process at the same time, and they can cooperate to complete a common task together, or they can do their own things. Whether collaborating or working alone, there is always a question that cannot be avoided: "how to allocate resources for CPU?" The process of competing for resources between multi-threads can be regarded as a "zero-sum game". Thread A grabs more resources, so thread B must get less resources. In general, users only care about the overall utilization of CPU, and the higher the value, the better, regardless of whether each thread gets more or less computing resources.
At this point, I believe that you have a deeper understanding of "how to understand the Linux I _ peg O mechanism". 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.