Understand IO Completion Port

xiaoxiao2021-03-06  43

Welcome to this IOCP tutorial. I will give the IOCP definition and give its implementation method, and finally analyze an echo program to open the IOCP puzzle cloud, remove your heart on IOCP. OK, but I can't guarantee that you understand everything IOCP, but I will do my best. The following is the related art I will mention in this article: I / O port synchronous / asynchronous blockage / non-blocking server / client multi-thread program Design Winsock API 2.0

Prior to this, I have developed a project, one of which needs to support the code, and only use SELECT, CONNECT, Accept, Listen, Send and Recv, plus a few #ifdef Package is used to deal with the incompatibility between Winsock and BSD socket [socket], a network subsystem is written for only a few hours, and I still make me a good aftertaste. It didn't touch it for a long time.

A few days ago, we plan to make a online game, I took the initiative to bear this piece, think about this is not a small CASE, I am stealing. Online games, online games provide fun and homologous game experiences for hundreds of players, they fight each other or join the team to overcome the common enemies. I am confident that I am ready to write my network, so I found that the past block synchronous mode mode can't get a huge number of multi-player [MMP] architectures, directly denied it. So, there is an IOCP. If you can easily lose IOCP, there will be this tutorial. Please follow me to enter the topic.

What is IOCP? Let's take a look at the I / O completion port for IOCP may be the most complex kernel object provided by Win32. [Advanced Windows 3rd] Jeffrey Richter This is the best way to implement high-capacity web servers. [Windows Sockets 2.0: Write Scalable Winsock Apps Using Completion Ports] Microsoft Corporation completed the port model to provide the best scalability. This model is very suitable to handle hundreds and even thousands of sockets. [Windows Network Programming 2nd] Anthony Jones & Jim Ohlundi / O Completion Ports is particularly important because they are the only technique for high load servers [must maintain many connection lines at the same time. The Completion Ports uses some threads to help balance the load caused by the I / O request. Such architectures are particularly suitable for "Scalable" servers generated in the SMP system. [Win32 multi-threaded program] Jim Beveridge & Robert Wiener

It seems that we have a complete reason to believe that IOCP is the first choice for large network architecture. What is the IOCP?

Microsoft introduced this concept of IOCP in Winsock2. IOCP full name I / O Completion port, Chinese translated into I / O completion port. IOCP is an Asynchronous I / O API that delivers the I / O event to the application efficiently. Unlike the use of select () or other asynchronous methods, a socket [socket] is associated with a completion port, and then proceeds to normal Winsock operations. However, when an event occurs, this completion port will be added to a queue by the operating system. The application can then query the core layer to get this complete port.

Here I am going to add some of the above concepts. Before explaining [Finish], I want to simply let the synchronization and asynchronous concepts, logically let another after doing one after another. It is to synchronize, while doing two or two things together is asynchronous. You can also take a single thread and multi-threaded metaphor. But we must separate synchronous and blockage, asynchronous, and non-blocking distal distalbsor, so-called clogging functions such as Accept (...), after calling this function, at this time thread will hang until the operating system is notified, "Hey Brothers Some people come in, "that the hangs will continue to work, which is also in line with the" producer-consumer "model. Blocking and synchronizing seems to have two points, but it is a completely different concept. Everyone knows that I / O equipment is a relatively slow device. Regardless of the printer, modem, even hard drives, compared with the CPU, it is uncomfortable, and the completion of I / O is a unwise thing. Sometimes the flow rate of data is very amazing, move the data from your file server with Ethernet speed, speed may be up to one million bytes per second, if you try to read 100KB from the file server, in the user's vision It is almost instantaneous completion, but you have to know that your thread executes this command, has been wasted 10 1 million CPU cycles. Therefore, we generally use another thread to perform I / O. Overlap IO [Overlapped I / O] is a technology of Win32, you can ask the operating system to transfer data for you and inform you when transferred. This is also the meaning of [completion]. This technology allows your program to continue to handle transactions during I / O. In fact, the inside of the operating system is threaded to complete overlapped I / O. You can get all the interests of threads without having to pay for the price of pain. Completing the so-called [port] in the port is not the port mentioned in TCP / IP, it can be said that it is completely no relationship. I didn't want to pass an I / O device [I / O DEVICE] and port [IOCP port]. It is estimated that this port is also confused. IOCP is only used to read and write operations, and it is similar to file I / O. Since it is a read and write device, we can ask for it just to deal with read and write efficient. The third part of the article You will easily discover the true intention of the IOCP design.

What is the relationship between IOCP and the network?

int main () {WSAStartup (MAKEWORD (2, 2), & wsaData); ListeningSocket = socket (AF_INET, SOCK_STREAM, 0); bind (ListeningSocket, (SOCKADDR *) & ServerAddr, sizeof (ServerAddr)); listen (ListeningSocket, 5) ; int nlistenAddrLen = sizeof (clientAddr); while (TRUE) {NewConnection = accept (ListeningSocket, (SOCKADDR *) & clientAddr, & nlistenAddrLen); HANDLE hThread = CreateThread (NULL, 0, ThreadFunc, (void *) NewConnection, 0, & dwTreadId) CloseHandle (hthread);} return 0;}

I believe that as long as you write a friend, you should be familiar with such a structure. The rear thread is hanged, waiting for a customer to issue a request, and then create a new thread to handle the request. When the new thread handles the customer request, the initial thread loops goes back to wait for another customer request. The thread processing that handles the customer request is ended. In the above concurrent model, a thread is created for each customer request. It is advantageous that the thread waiting for the request only needs to do very little work. Most time, the thread is in sleep [because the RECV is in a plug state].

But when the concurrent model is applied to the server side [Windows NT], the Windows NT team notes that the performance of these applications is unpredictable. Special, handling a lot of customer requests means that many threads are running concurrently in the system. Because all these threads are running [not hanging and waiting for something], Microsoft realizes that the NT core takes too much time to convert the context of running thread [context], threads have not got a lot of CPU time. Do their work.

Everyone may also feel the bottleneck of parallel models in it created a new thread for each customer request. Creating a thread is smaller than the creation process overhead, but it is far from not overhead.

We may wish to imagine: If you have a good N thread in advance, let them in the HOLD [Clog], then you can deliver all users' requests to a message queue. Then the N thread takes the message from the message queue one by one. You can avoid the shutdown of each user request. Not only reduces the resources of threads, but also improves the utilization rate of threads. Theory is very good, you think that I can think about the problem, how will Microsoft not take into account?!

The solution to this problem is a kernel object called I / O completed port, he first introduced in Windows NT3.5.

In fact, our idea should be almost the design mechanism of IOCP. In fact, it is not a message queue! You said this and [port] What are you connected? My understanding is that IOCP is an interface that is communicated with the application and operating system.

As for the specific design of IOCP, I am also very difficult to say, after all, I haven't seen the code implemented, but you can simulate it, but you may ..., if you want to understand the IOCP, Jeffrey Ritchter Advanced Windows 3rd in which 13 Chapters and 14th have a lot of valuable content, you can take a look at how the system is all.

Implementation

Microsoft provides corresponding API functions for IOCP, mainly two, let's take a look at: Handle FileHandle, // Handle to File Handle ExistingCompletionport, // Handle To I / O Completion Port Ulong_ptr completionKey, / / Completion Key DWORD NUMBEROFCONCURRENTTHREADS / / NUMBER OF Threads to Execute Concurrently

Before discussing each parameter, you should first pay attention to the actual use of two distinct purposes: 1. Used to create a completed port object 2. Associate a handle [Handle] and complete port together

When you create a complete port, we only need to fill in the parameter of NumberOfConcurrentThreads. It tells the system a maximum number of threads that are allowed to operate simultaneously on the port. By default, the number of wires is the same, but the number of CPUs is the same, but the experience gives us a formula: thread number = CPU number * 2 2 To make the completed port is useful, you must associate it the same or more devices. This is also done by calling Createiocompletionport. You have to pass an existing completion of the port to this function. We have to handle the online event, that is, the customer's socket is passed as a Handle. And a completed button [a 32-bit value for you, it is a pointer, the operating system doesn't care what you are. When you associate a device to the port, the system adds a message to the device list of the completion port. Another API is BOOL GetQueuedCompletionStatus (HANDLE CompletionPort, // handle to completion port LPDWORD lpNumberOfBytes, // bytes transferred PULONG_PTR lpCompletionKey, // file completion key LPOVERLAPPED * lpOverlapped, // buffer DWORD dwMilliseconds // optional timeout value);

The first parameter pointed out which completion port is to be monitored. Many service applications are just using an I / O completion port, and all I / O requests are completed, and they will be sent to this port. Simply put, getQueuedCompletionStatus causes the calling thread to hang until one of the specified port I / O completes a queue or until the timeout. The third data structure associated with the I / O completion port is to make the thread get information in completing the I / O item: the number of bytes transmitted, the completion keys, and the address of the overlapped structure. This information is returned to thread by passing to the LPDWNumberofByTestransferred, LPDWCompletionKey and LPOVERLAPPED parameters of GetQueuedCompletionsatus.

According to the things that have already been mentioned so far, first build a frame. Let you explain how to develop an Echo server using the completed port. Based on: 1. Initialize Winsock 2. Create a completion port 3. Create a certain amount of threads depending on the server thread 4. Prepare a socket for bind then Listen 5. Enter Cyclic Accept Waiting for the customer request 6. Create a data structure Socket and other related information 7. Connect the Socket associated with the completion port 8. After delivery, you will continue to repeat 5 to 8 processes, we will use the specific code to show the details of the details.

Wow, the code of the program is attached here, it is really a scenery, can't Ctrl V can't f7. If you need the source code, you can send it to me o_nono@163.net

To this article, I should have a fall, I took you a whirlwind tour, visited the so-called completed port. Many details cannot be detained in detail due to the relationship between the space. But I hope this article will bring you more thinking. If you have any questions, you can send it to o_nono@163.net.

转载请注明原文地址:https://www.9cbs.com/read-55018.html

New Post(0)