Linux high-performance server programming reading notes

TCP/IP protocol family

Detailed TCP protocol

Characteristics of TCP service

  • Connection-oriented, byte stream
  • Reliable transmission
    Both parties who use the TCP protocol to communicate must first establish a connection before reading and writing data. Only then can the reading and writing of data begin. Both parties must allocate the necessary kernel resources for the connection to manage the state of the connection and the transmission of data on the connection. The TCP connection is full duplex. That is, data reading and writing of both parties can be carried out through a connection. After completing the data exchange, both parties must disconnect to release system resources.

Linux network basic API

Data read and write

TCP data read and write

#include<sys/types.h>
#include<sys/socket.h>
ssize_t recv(int sockfd,void *buf,size_t len,int flags);
ssize_t send(int sockfd,const void *buf,size_t len,int flags);

recv reads the data above sockfg, the buf and len parameters respectively specify the location and size of the read buffer. Flags is usually set to 0. When recv succeeds, it returns the length of the data actually read, which may be less than the length len, so it may need to call recv multiple times to read the complete data. recv may return 0, which means that the communication partner has been closed. When recv fails, it returns -1 and sets errno.
send writes data to sockfd. The buf and len parameters specify the location and size of the write buffer respectively. When send succeeds, it returns the length of the data actually written, and when it fails, it returns -1 and sets errno.

Advanced I/O functions

pipe function

The pipe function can be used to create a pipe. To achieve inter-process communication. It returns 0 on success and -1 on failure.

#include<unistd.h>
int pipe(int fd[2]);

The two file descriptors fd[0] and fd[1] created by the pipe function constitute the two ends of the pipe respectively. The data written to fd[1] can be read from fd[0]. Also, fd[0] can only read data from the pipe, and fd[1] can only write data to the pipe. It cannot be used the other way around. If you want to achieve two-way data transmission, you should use two pipes. By default, this pair of file descriptors are blocked. At this time, if you use read to read an empty pipe. Then read will be blocked. Until there is data readable in the pipeline. If you use write to write data to a full pipe. Then write will also be blocked. Until the pipeline has enough free space available.
The pipeline itself has a capacity limit. It specifies if the application does not read the data from the pipe. The maximum number of bytes of data that can be written to the pipe. The capacity of the pipe can be fcntlmodified through the function

#include<sys/types.h>
#include<sys/socket.h>
int socketpair(int domain,int type,int protocol,int fd[2]):

The domain in socketpair can only use the UNIX local domain protocol family AF_UNIX, because this two-way pipe can only be used locally. At this time, the fd can be read or written

dup and dup2 functions

The dup and dup2 functions can redirect standard input to a file or direct standard output to a network connection.

#include<unistd.h>
int dup(int fd);
int dup2(int fd1,int fd2):

The dup function creates a new file descriptor. The new file descriptor and the original descriptor fd point to the same file, pipe or network connection. And the file descriptor returned by dup always takes the smallest integer value currently available in the system. dup2 is similar to dup, but it will return the first integer value not less than fd2. Both return -1 and set errno when the call fails.
Note: The file descriptor created by dup and dup2 does not inherit the attributes of the original file descriptor. For example, close-on-exec and non-blocking.

Linux server program specification

Server programming framework

Single server program
I/O processing unit: handles client connections, reads and writes network data,
logical unit: business process or thread,
network storage unit: local database, file or cache
request queue: communication method between units,
server cluster
I/O processing Unit: As an access server, realizing load balancing.
Logic unit: Logical server
Network storage unit: Database server
request queue: Permanent TCP connection between servers

I/O processing unit

The server manages the client connection module.

Duty

Wait and accept the new client connection, accept the client data, and return the server response to the client. However, the sending and receiving of data is not necessarily performed in the I/O processing unit. It may also be executed in a logical unit, and where it is executed depends on the event processing mode. For a server cluster, the I/O processing unit is a dedicated access server, which implements load balancing, and selects the least-loaded one from all logical servers to serve new customers.

Logical unit

A logical unit is usually a process or thread. It analyzes and processes customer data, and then passes the results to the I/O processing unit or directly to the client (which method is used depends on the event processing mode). For a server cluster, a logical unit itself is a logical server. Servers usually have multiple logical units. In order to realize the parallel processing of multiple client tasks.

Network storage unit

It can be database, cache, and file. Even a separate server, but it is not necessary. For example, login services such as ssh and telent don't need this unit.

Request queue

It is an abstraction of the way of communication between units. When the I/O processing unit receives a client request, it needs to notify a logic unit in some way to process the request. Similarly, when multiple logic units access a storage unit at the same time, some mechanism is also needed to coordinate the handling of race conditions. The request queue is usually implemented as part of the pool. For the server cluster, the request queue is a pre-established, static and permanent TCP connection between each server

I/O model

The socket is blocked by default when it is created. You can pass the SOCK_NONBLOCKflag to the second parameter of the socket system call , or set it to non-blocking through the F_SETFL command of the fcntl system call.
Block I/O
blocked file descriptors. System calls executed for this type of I/O may be suspended by the operating system because they cannot be completed immediately. Until the waiting event occurs. In the basic socket API, the system calls that may be blocked are:

  • accept
  • send
  • recv
  • connect
    non-blocking I/O
    non-blocking file descriptor. System calls executed for this type of I/O always return immediately. Regardless of whether the event has occurred. If the event does not occur immediately, the system call will also return.
    Summary
    Obviously, only operating non-blocking I/O (read, write, etc.) when the event has occurred can improve the efficiency of the program. Therefore, non-blocking I/O usually needs to be used with other I/O mechanisms. For example, I/O multiplexing and SIGIO signals.

I/O multiplexing

The application program registers a set of events with the kernel through the I/O multiplexing function, and the kernel notifies the application of the ready events through the I/O multiplexing function. Commonly used I/O multiplexing functions on Linux are select, poll and epoll_wait.
Note: I/O multiplexing functions themselves are blocking. The reason they can improve program efficiency is that they have the ability to monitor multiple I/O events.
I/O events

Guess you like

Origin blog.csdn.net/weixin_39308337/article/details/108522820