In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to analyze the Linux-I/O model, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Introduction to Ipaw O
There are usually memory IO, network IZP O, disk IZUP O and so on, but we usually talk about network IZUP O and disk IZUBO. Network Icano: essentially socket read
Each Icano request consists of two stages: the first step: wait for data, that is, data from disk to kernel memory; load data from disk file to kernel memory space (buffer) first, and wait for data preparation to be completed, which takes a long time. The second step: copy the data, that is, the data kernel memory to the process memory; copy the data from the kernel buffer to the process memory in user space in a short time.
Web request processing process
1. The client initiates the case to the server network card
two。 The server network card receives the request and transfers it to the kernel for processing.
3. According to the socket corresponding to the request, the kernel hands the request to the Web server process working in user space.
According to the user's request, the 4.Web server process makes a system call to the kernel and applies for obtaining the corresponding resources (for example, the client gets the picture)
5. The kernel finds that the Web server process requests a resource stored on the local hard disk, so it connects to the disk through the driver
6. The kernel calls the disk to get the resources you need
7. The kernel stores resources in its own cache and notifies the Web server process
The 8.Web server process takes the resource through the system call and copies it into the process's own buffer
The 9.Web server process forms a response and sends it to the kernel again through the system call to respond to the request
10. The kernel sends the response to the network card
11. The network card sends a response to the user
Through such a complex process, a single request is completed.
To put it simply:
User request-- "deliver user space-- > system call--" kernel space-- > kernel reads picture resources on disk-- "return to user space-- > respond to the user.
The above simple explanation, the client requests to the Web server process, in this process, there are two processes: one is the client request network Imando O, the other is the Web server request image disk Imando O.
Introduction to the nouns of Ipaw O model
When it comes to the Icano model, synchronous, asynchronous, blocking, and non-blocking words are involved. The following explains the concepts of these words.
Blocking and non-blocking
Blocking and non-blocking means that when performing an operation, the result is returned after the operation is completed, or immediately.
Blocking: refers to the fact that the IO operation needs to be completed before returning to user space. Before the result of the call is returned, the caller is suspended (the current thread enters a non-executable state, in which the CPU does not allocate time slices, and the thread suspends running). Only when the result is returned does the caller enter the active state.
An example of blockage: the server in Haidilao orders for you. When you finish ordering, the waiter sends the message to the kitchen, and you wait on the table until the cook sends the soup pot and side dishes to your table. You can't leave during the serving process, because after you leave, the waiter serves the food but can't find you, so you can just wait, at this time you are in a blocking waiting state, that is, as mentioned earlier, you are the caller, you are suspended, and you are in a non-executable state.
Non-blocking (nonblocking): a status value is returned to the user immediately after the operation is called. There is no need to wait until the operation is completed and the caller will not be suspended until the final call result is returned.
Non-blocking example: the server in Haidilao ordered for you. When you finished ordering, the waiter passed the news to the back kitchen. After three minutes, you ran to the back kitchen and asked, have you finished my pot or fat cow roll? The back chef said no, and then you went to take care of other things, and then after five minutes, you went to the back kitchen and asked, "is one of my dishes ready? if not, you should continue to do other things, and then ask again later. At this time, while you are not suspended, you can operate other things, but if the Icano operation is completed, you need to accept it immediately."
Synchronous and asynchronous
Synchronous / asynchronous focus on message communication mechanism
Synchronous: the caller waits for the callee to return a message before continuing execution. Example of synchronous blocking: go to a restaurant, order a covered rice, then wait on the table until the covered rice is ready, and bring it to the table yourself. This is a typical synchronous blocking. When the cook cooks for you, you need to wait there all the time.
Synchronous non-blocking example: go to a restaurant and order a covered rice. After you order the meal and feel that the time is almost up in a few minutes, ask the boss if the meal is ready. If it is ready, go and ask later, synchronize the cooking progress in real time, and cycle through until the meal is ready. This is synchronous non-blocking. Asynchronous: the callee actively notifies the callee of the callee's running status through the status, notification, or callback mechanism.
Ipaw O model type
IO models are divided into the following five categories
1. Blocking type: all processes are fully blocked
two。 Non-blocking: if there is no data buffer, return EWOULDBLOCK immediately
3.I/O multiplex (select and poll): blocking in the wait and copy phases, respectively
4. Signal-driven SIGIO: no blocking in the wait phase, but blocking in the copy phase (signal-driven Ihamo), i.e. notification
5. Asynchronous I _ AIO: a completely non-blocking mode that provides a signal when it is completed.
Blocking IPUBO
Note: when an application calls a recvfrom function of IO, it will cause the application to block. After entering the blocking state, the application will not return until the end of the Icano operation. If the kernel data of the system is not ready, then wait for the data to be prepared, because it is the call to the recvfrom function that causes the application to block, so you have been waiting and can't do anything. After the kernel data is ready, the data is copied from the kernel to the user space. After the copy is finished, the Igamot O function returns a success indication. Note: when it is blocked, it is in the operation stage of Istroke O.
Non-blocking IPUBO
Description: the user thread returns immediately when it initiates an IO request. However, if no data is read, the return field is "EWOULDBLOCK". The user thread needs to make IO requests constantly until the data arrives. That is, "polling" mechanism. Throughout the IO request, although the user thread can return immediately after each IO request, but in order to wait for the data. It still requires constant polling, repeated requests, and consumes a lot of CPU resources; it is a wasteful way of CPU, and this model is rarely used, but the feature of non-blocking IO is used in other models.
IPot O multiplexing (select and poll)
Note: the select or poll function will be used in the Istroke O reuse model, in which the block is not in the process of the operation, but in the select or poll function. Take select as an example: the process blocks at select, waiting for one of several descriptors to become operable, if it does not wait, it continues to block in the first stage, and if a descriptor becomes operable, the recvfrom function is called to copy the data to the application buffer.
Signal-driven Icano (SIGIO)
Note: first of all, we allow the socket to signal-drive Imax O and install a signal processing function SIGIO. If the data is not ready, the result is returned immediately, and the process continues to work without blocking. When the data is ready, the kernel of the system will actively send a SIGIO signal to the application. After the application receives the signal, it can call recvfrom in the signal processing function to process the data. The advantage of the signal-driven Icano model is that when the Datagram arrives, it can be unblocked, the main loop can continue to execute, just wait for the handler to notify, or the data is ready to be processed, or the Datagram is ready to be read.
Asynchronous IBG O (AIO)
Description: when an asynchronous procedure call is made, the caller cannot get the result immediately. When the part that actually handles the call is finished, it tells the caller the input and output operation through status notification and callback notification. Users can directly perform read and write operations on IWeiO, which tells the kernel where the read-write buffer is located and how the kernel notifies the application after the IWeiO operation is completed, that is, it tells the caller through status notification or callback notification. The read and write operation of the asynchronous Iripple O always returns immediately, but no result is returned as to whether it is blocked or not, because the real read and write operation of the asynchronous Iripo operation has been taken over by the kernel, and the kernel itself generates a signal after the data processing is completed, and then informs the user that the event just handed to him has been completed.
Summary and comparison of five Icano models
The picture in Chinese is as follows:
The English illustration is as follows:
As we can see from the two pictures, the later there is less congestion, and the efficiency is the best in theory. Among the five Icano models, the first three belong to synchronous Icano and the latter two belong to asynchronous Icano.
Synchronous Ipaw O
Blocking I _ paw O non-blocking I _ sign O multiplexing (select and poll)
Asynchronous IPUBO
Signal-driven SIGIO semi-async AIO fully async
The difference between Asynchronous Iripple O and signal-driven Igamot O
When the kernel can be copied in the signal-driven IPUP O mode, the SIGIO signal is sent to our application when it can be copied. In the asynchronous Ihop O mode, the kernel will not notify our application until all the operations have been completed by the kernel.
The above is how to parse the Linux-I/O model. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.