High-performance Linux server programming: high performance server framework

The server has three main modules:

(1) I / O processing unit

(2) the logic unit

(3) The storage unit

 

1. server model

C / S model

Logic: After the server is started, first create one or more listening socket, and calls the bind function to bind it to a port on the server of interest, and then call listen function waits for client connections.

After the server is stable, the client can call the connect function initiates a connection to the server.

P2P model

P2P model so that each machine can also provide service to others while consuming services, so that resources can be fully shared freedom.

 

2. The server programming framework

contain:

I / O request queue logic processing unit unit requests network store queue

I / O processing unit: processing client connection, network data write

Logical unit: business processes or threads

Network storage units: a local database, or file cache

Request queue: communication between units

 

3. I / O model

System call blocking I / O execution may not be completed immediately because the operating system is suspended, know to wait until the event occurs. Potentially blocking system calls include: accept, send, recv and connect.

Non-blocking I / O system call execution is always returned immediately, regardless of the time occurs. If the event does not take place immediately, these system calls return -1, and error situation the same. We must distinguish between these two cases according to errno.

To accept, send and recv, the event does not occur when the usually errno is set to EAGAIN or EWOULDBLOCK: to connect, the errno were set to EINPROGRESS.

 

Obviously, only operate non-blocking I / O in the case of an event has occurred, in order to improve the efficiency of the program. I / O notification mechanisms: I / O and multiplexed SIGIO signals.

I / O multiplexing: the application through I / O multiplexing up to the kernel function of a set of events, a function to which the ready event notification to the application by the kernel complex I / O. On Linux common I / O functions are multiplexed selcet, poll and epoll_wait. The reason I / O multiplexing itself is blocked, energy efficiency is

They have the ability to simultaneously monitor multiple I / O events.

SIGIO signal: You can specify a target file descriptor for the hosting process, then the designated host process to capture SIGIO signal. Thus, when there is the target file descriptor event occurs, the signal processing function SIGIO signal to be triggered, may be performed non-blocking I / O operations to a target file descriptor in the signal handler.

 

Theoretically, blocking I / O, I / O multiplexing and signal driven I / O is I / O synchronization model. In the three models, the read and write I / O's, after all I / O events, accomplished by the application.

Asynchronous I / O, the user can direct I / O read and write operations, read and write buffers position after these operations the user to tell the kernel, and the I / O operation to complete the kernel notifies the application manner, asynchronous I / O read and write operations always return immediately, regardless of I / O if it is blocked, because the real read and write operations have been taken over by the kernel.

That is, synchronous I / O model requires user code I / O operations on their own (the data from the kernel buffer into the user buffer is read, or write data to the user buffer from the kernel buffer), and the asynchronous I / O mechanism by the kernel to perform I / O operations (moving data between user and kernel buffer by the buffer in the kernel "background" complete).

Synchronous I / O to the notice of application is I / O ready event, and asynchronous I / O notification to the application is I / O completion events.

 

4. The two kinds efficient event processing mode

Synchronous I / O model implemented Reactor pattern:

(1) the main thread to epoll kernel event registration table read on the socket ready event.

(2) wait for the main thread calls epoll_wait readable data socket.

(3) when there is data to be read on the socket, epoll_wait notify the main thread, the main thread requests the socket into the event queue.

(4) sleep in the wake of a worker on the request queue, the data is read from the socket, and process customer requests and to epoll kernel event registration table to write the event's ready event.

(5) the main thread calls epoll_wait wait socket is writable.

(6) when the socket can write, epoll_wait notify the main thread. The socket main thread can write event into the request queue.

(7) sleep wake up at the request of a worker on the queue, the result of server processes client requests it writes to the socket.

 

Proactor mode

Proactor mode all I / O operations to the main thread and the kernel to handle, the worker thread only responsible for business logic.

 

Semi-synchronous / asynchronous half:

Thread synchronization logic for handling customers, asynchronous thread used to process I / O events.

Asynchronous thread acts as the main thread. It is responsible for monitoring all events on the socket. If the monitor has a socket readable event, that is the arrival of a new connection, it is accepted by the main thread to get a new connection socket, then register read and write events on the socket to epoll kernel event table.

If there is a connection socket write event, i.e., the arrival of new client requests or data to be sent to the client, the main thread is inserted into the socket connection request queue. All the worker threads are sleeping on the request queue, when a task to bring, will get the right to take over the task through competition.

Disadvantages:

The main thread and the worker threads sharing request queue. The main thread request queue is added to the task or tasks worker thread removed from the request queue, need protection lock request queue, thereby wasting CPU time bye.

Each worker at the same time can only handle a client request.

 

Guess you like

Origin www.cnblogs.com/sssblog/p/12376464.html