Socket programming | Concurrent blocking of TCP server IO model (multi-process implementation)-3

Concurrent blocking of TCP server IO model

1 Introduction

There are many multi-process applications in the Linux environment, the most important of which is the network/client server. The multi-process server is that when the client has a request, the server uses a child process to process the client request. The parent process continues to wait for requests from other clients. The advantage of this method is that when the client has a request, the server can process the client in time, especially in a client-server interactive system. For a TCP server, the connection between the client and the server may not be closed immediately, but may be closed after the client submits some data. During this time, the server-side process will be blocked, so the operating system may schedule other client service processes at this time. Compared with the round-robin server, the service performance is greatly improved.

2. Implementation ideas of multi-process concurrent server

Based on the most primitive blocking network I/O, if the server needs to support multiple clients, the more traditional way is to use the multi-process model , that is, assign a process to each client to process the request.

The main process of the server is responsible for monitoring the connection of the client. Once the connection with the client is completed, the accept() fork()function Everything is copied , including file descriptors, memory address space, program counter, executed code, etc.

When the two processes have just been copied, they are almost exactly the same. However, it will distinguish whether it is the parent process or the child process according to the return value . If the return value is 0, it is the child process; if the return value is other integers, it is the parent process.

Because the child process will copy the file descriptor of the parent process , it can directly use the "Connected Socket" to communicate with the client.

It can be found that the child process does not need to care about "monitoring

Guess you like

Origin blog.csdn.net/weixin_40209493/article/details/129161129