"Linux Programming high-performance server," read the note "Linux high-performance server program" Reading notes

"Linux high-performance server program" Reading notes

  • bind return 0 on success, -1 on failure and set errno. Among them, two common errno is EACCES and EADDRINUSE, their meanings are:
    • EACCES : address is bound is protected address, only the super user can access
    • EADDRINUSE : address is bound is in use.
  • listen to backlog parameter indicates: the upper limit of the socket in the fully connected state.
  • accept just removed from the queue listener is connected, regardless of what state the connection. ( ESSTABLISHED or CLOSE_WAIT )
  • connect return -1 on failure and set errno. Among them, the two most common errno is ECONNREFUSED and ETIMEDOUT .
    • ECONNREFUSED The : destination port does not exist, the connection is refused.
    • ETIMEDOUT : Connection timed out.
  • close is not immediately close a connection, but the reference count is decremented fd.

    Focus here:

  • For listening Socket , in listen setting these common Socket options before calling, then return Accept the connection Socket will inherit these options: SO_KEEPALIVE , SO_LINGER , SO_RCVBUF , SO_REVLOWAT , SO_SNDBUF , SO_SNDLOWAT , TCP_NODELAY .
  • For the client Socket , the above options should call Connect before setting function, because after Connect call returns successfully, TCP three-way handshake has been completed.
  • Below I will detail these socket options:
    • The SO_REUSEADDR : server programs can connect into occupied TIME_WAIT state Socket Socket Address by setting the option to enforce the use of
    • SO_REUSPORT : allows multiple instances to start a service on a port, as long as each instance is bound different local IP address.
    • SO_RCVBUF  and  SO_SNDBUF : TCP or accept the size of the buffer and send buffer. TCP receive buffer size is preferably set to MSS (1460) is an even multiple.
    • SO_RCVLOWAT  and  SO_SNDLOWAT : acceptance of low bit flag buffer and the send buffer. By default, both 1.
    • SO_LINGER : Close control system can be invoked through the different options of behavior. By default, when we use the Close system call to close a Socket, Slose return immediately, TCP Socket module is responsible for sending the data buffer is sent to the other party.
    • SO_KEPPALIVE : send periodic keep-alive packet to maintain the connection.
    • TCP_NODELAY : Prohibition Nagle algorithm, that will be a lot of small blocks of data on the connection, you can turn in a LAN environment.
  • Zero-copy function:
#include<sys/sendfile.h>
ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count);
  • Zero-copy function:
#include<fcntl.h>
ssize_t splice(int in_fd, loff_t* off_in, int out_fd, loff_t* off_out, size_t len, unsigned int flags); //使用splice实现零拷贝反射服务器 int pipefd[2]; int ret = pipe(pipefd); ret = splice(connectfd, NULL, pipefd[1], NULL, 65536, SPLICE_F_MORE | SPLICE_F_MOVE); ret = splice(pipefd[0], NULL, connectfd, NULL, 65536, SPLICE_F_MORE | SPLICE_F_MOVE); close(connectfd);
  • For non-blocking IO system calls always return immediately, regardless of what has occurred. If things do not happen immediately, these system calls immediately return -1 on error and return the same time we must judge what happens according to specific errno set when returned. To Accept, Send, Recv , the incident did not occur when errno is usually set to EAGAIN (meaning "again") or EWOULDBLOCK (meaning "expectation blocking"); for Connect , the errno were set to EINPEOCESS (means "in process").
  • The reason IO multiplexing function itself is blocked, they can improve the efficiency of the program is that they have the ability to listen to multiple colleagues IO events.
  • Server programs typically need to deal with three types of events: IO event , timer event , signal event .
  • Two event processing mode: the Reactor and Proator
    • Reactor
      • The main thread to Epoll kernel event registration table readable event
      • The main thread waits readable event occurs
      • Readable event occurs, the main thread readable event into the request queue
      • The main thread will sleep in the request queue worker thread wakeup processing readable event . After processing is complete, the registration Epoll can write event
      • The main thread waits can write event occurred
      • Can write event occurs, the main thread to write the event into the request queue
      • This back and forth cycle
    • Proator
      • When the main thread to complete the event up to the kernel read, complete, signaled application
      • The main thread to continue processing other logic
      • Read main thread completion event is received, the read data is encapsulated into an event object into the worker thread. After the worker thread processing is completed, the completion of the event up to the kernel to read, to tell the kernel and user buffer location
      • The main thread to continue processing other logic
      • The main thread reads receive completion event, do deal with the aftermath
      • This back and forth cycle
  • Three IO multiplexing mechanism
    • the SELECT : After each incident, the registration will be modified before the event, the need for judgment FD_ISSET, and then re-register.
    • poll : the same select, using polling mechanism
    • the epoll : the epoll on the file descriptor has two modes: LT (trigger level) and the ET (edge triggered). LT mode is the default mode of operation. When set EPOLLET time, epoll will ET mode to a file descriptor, ET is a highly efficient mode of operation.
      • ET: When epoll_wait detected on an event occurs and the event notification to the application, the application must handle the event immediately, because the subsequent epoll_wait not notified of this event to the application again. Reducing the number of epoll event is triggered, and therefore more efficient than LT.
      • LT: epoll_wait detects an event occurs and the event notification application, the application can not handle the event immediately. In this way, when the next application of the first call epoll_wait, epoll_wait will notify the application of the event until the event is processed again.
      • EPOLLONESHOT : file descriptor for registered EPOLLONESHOT event, the operating system up to a trigger event for its registered (read, write or abnormal), and triggers only once, unless we use epoll_ctl function to reset the file descriptor register EPOLLONESHOT event.
        `` `

        include<sys/select.h>

        int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout);

FD_ZERO(fd_set* fdset);
FD_SET(int fd, fd_set* fdset);
FD_CLR(int fd,fd_set* fdset);
int FD_ISSET(int fd,fd_set* fdset);

timeval struct
{
Long the tv_sec; // seconds
long tv_usec; // microseconds
}

include<poll.h>

int poll(struct pollfd* fds, nfds_t nfds, int timeout);

the pollfd struct
{
int FD;
Short Event; // registered event
short revent; // event actually occurs, the core is filled, it is determined after each zero remember
}

POLLIN: Data read
POLLOUT: data can be written
POLLREHUP: TCP connection is closed each other, or the other closed writes
POLLERR: Error
POLLHUP: Suspend
POLLNVAL: no open file descriptor

include<sys/epoll.h>

int epoll_create(int size);
int epoll_ctl(int epollfd, int op, int fd, struct epoll_event* event);
int epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout);

op type parameters:
EPOLL_CTL_ADD: fd event to event registered in the registry
EPOLL_CTL_MOD: modified event registration on fd
EPOLL_CTL_DEL: delete the event registered on fd

struct epoll_event
{
_uint32_t events;
epoll_data_t data;
};

epoll_data Union
{
void * PTR;
int FD; // the specified event is subordinate descriptors
uint32_t U32;
uint64_t U64;
} epoll_data_t;

- 服务器端处理信号的方式:

void sig_handler(int sig)
{
int save_errno = errno;
int msg = sig;
send(pipefd[1], (char*)&msg, 1, 0);
errno = save_errno;
}

addsig void (int sig)
{
struct sigaction sa;
memset (& a, '\ 0', sizeof (a));
sa.sa_handleer = sig_handler;
sa.sa_flags | = SA_RESTART;
sigfillset (& sa.sa_mask);
assrt (sigaction (sig, & on, NULL)! = -1);
}
`` `

  • Network-related programming signals:
    • SIGHUP : Suspend control terminal process, SIGHUP signal will be triggered.
    • SIGPIPE : Into a closed conduit Socket or write data. The default state is the end of the process.
    • Of SIGURG : band data arrival.
    • SIGCHLD : child process state changes, use the wait for processing
    • SIGTERM : terminate the process. The default kill command transmits the signal.
    • SIGINT : keyboard input to interrupt the process.
 
  • bind return 0 on success, -1 on failure and set errno. Among them, two common errno is EACCES and EADDRINUSE, their meanings are:
    • EACCES : address is bound is protected address, only the super user can access
    • EADDRINUSE : address is bound is in use.
  • listen to backlog parameter indicates: the upper limit of the socket in the fully connected state.
  • accept just removed from the queue listener is connected, regardless of what state the connection. ( ESSTABLISHED or CLOSE_WAIT )
  • connect return -1 on failure and set errno. Among them, the two most common errno is ECONNREFUSED and ETIMEDOUT .
    • ECONNREFUSED The : destination port does not exist, the connection is refused.
    • ETIMEDOUT : Connection timed out.
  • close is not immediately close a connection, but the reference count is decremented fd.

    Focus here:

  • For listening Socket , in listen setting these common Socket options before calling, then return Accept the connection Socket will inherit these options: SO_KEEPALIVE , SO_LINGER , SO_RCVBUF , SO_REVLOWAT , SO_SNDBUF , SO_SNDLOWAT , TCP_NODELAY .
  • For the client Socket , the above options should call Connect before setting function, because after Connect call returns successfully, TCP three-way handshake has been completed.
  • Below I will detail these socket options:
    • The SO_REUSEADDR : server programs can connect into occupied TIME_WAIT state Socket Socket Address by setting the option to enforce the use of
    • SO_REUSPORT : allows multiple instances to start a service on a port, as long as each instance is bound different local IP address.
    • SO_RCVBUF  and  SO_SNDBUF : TCP or accept the size of the buffer and send buffer. TCP receive buffer size is preferably set to MSS (1460) is an even multiple.
    • SO_RCVLOWAT  and  SO_SNDLOWAT : acceptance of low bit flag buffer and the send buffer. By default, both 1.
    • SO_LINGER : Close control system can be invoked through the different options of behavior. By default, when we use the Close system call to close a Socket, Slose return immediately, TCP Socket module is responsible for sending the data buffer is sent to the other party.
    • SO_KEPPALIVE : send periodic keep-alive packet to maintain the connection.
    • TCP_NODELAY : Prohibition Nagle algorithm, that will be a lot of small blocks of data on the connection, you can turn in a LAN environment.
  • Zero-copy function:
#include<sys/sendfile.h>
ssize_t sendfile(int out_fd, int in_fd, off_t* offset, size_t count);
  • Zero-copy function:
#include<fcntl.h>
ssize_t splice(int in_fd, loff_t* off_in, int out_fd, loff_t* off_out, size_t len, unsigned int flags); //使用splice实现零拷贝反射服务器 int pipefd[2]; int ret = pipe(pipefd); ret = splice(connectfd, NULL, pipefd[1], NULL, 65536, SPLICE_F_MORE | SPLICE_F_MOVE); ret = splice(pipefd[0], NULL, connectfd, NULL, 65536, SPLICE_F_MORE | SPLICE_F_MOVE); close(connectfd);
  • For non-blocking IO system calls always return immediately, regardless of what has occurred. If things do not happen immediately, these system calls immediately return -1 on error and return the same time we must judge what happens according to specific errno set when returned. To Accept, Send, Recv , the incident did not occur when errno is usually set to EAGAIN (meaning "again") or EWOULDBLOCK (meaning "expectation blocking"); for Connect , the errno were set to EINPEOCESS (means "in process").
  • The reason IO multiplexing function itself is blocked, they can improve the efficiency of the program is that they have the ability to listen to multiple colleagues IO events.
  • Server programs typically need to deal with three types of events: IO event , timer event , signal event .
  • Two event processing mode: the Reactor and Proator
    • Reactor
      • The main thread to Epoll kernel event registration table readable event
      • The main thread waits readable event occurs
      • Readable event occurs, the main thread readable event into the request queue
      • The main thread will sleep in the request queue worker thread wakeup processing readable event . After processing is complete, the registration Epoll can write event
      • The main thread waits can write event occurred
      • Can write event occurs, the main thread to write the event into the request queue
      • This back and forth cycle
    • Proator
      • When the main thread to complete the event up to the kernel read, complete, signaled application
      • The main thread to continue processing other logic
      • Read main thread completion event is received, the read data is encapsulated into an event object into the worker thread. After the worker thread processing is completed, the completion of the event up to the kernel to read, to tell the kernel and user buffer location
      • The main thread to continue processing other logic
      • The main thread reads receive completion event, do deal with the aftermath
      • This back and forth cycle
  • Three IO multiplexing mechanism
    • the SELECT : After each incident, the registration will be modified before the event, the need for judgment FD_ISSET, and then re-register.
    • poll : the same select, using polling mechanism
    • the epoll : the epoll on the file descriptor has two modes: LT (trigger level) and the ET (edge triggered). LT mode is the default mode of operation. When set EPOLLET time, epoll will ET mode to a file descriptor, ET is a highly efficient mode of operation.
      • ET: When epoll_wait detected on an event occurs and the event notification to the application, the application must handle the event immediately, because the subsequent epoll_wait not notified of this event to the application again. Reducing the number of epoll event is triggered, and therefore more efficient than LT.
      • LT: epoll_wait detects an event occurs and the event notification application, the application can not handle the event immediately. In this way, when the next application of the first call epoll_wait, epoll_wait will notify the application of the event until the event is processed again.
      • EPOLLONESHOT : file descriptor for registered EPOLLONESHOT event, the operating system up to a trigger event for its registered (read, write or abnormal), and triggers only once, unless we use epoll_ctl function to reset the file descriptor register EPOLLONESHOT event.
        `` `

        include<sys/select.h>

        int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout);

FD_ZERO(fd_set* fdset);
FD_SET(int fd, fd_set* fdset);
FD_CLR(int fd,fd_set* fdset);
int FD_ISSET(int fd,fd_set* fdset);

timeval struct
{
Long the tv_sec; // seconds
long tv_usec; // microseconds
}

include<poll.h>

int poll(struct pollfd* fds, nfds_t nfds, int timeout);

the pollfd struct
{
int FD;
Short Event; // registered event
short revent; // event actually occurs, the core is filled, it is determined after each zero remember
}

POLLIN: Data read
POLLOUT: data can be written
POLLREHUP: TCP connection is closed each other, or the other closed writes
POLLERR: Error
POLLHUP: Suspend
POLLNVAL: no open file descriptor

include<sys/epoll.h>

int epoll_create(int size);
int epoll_ctl(int epollfd, int op, int fd, struct epoll_event* event);
int epoll_wait(int epfd, struct epoll_event* events, int maxevents, int timeout);

op type parameters:
EPOLL_CTL_ADD: fd event to event registered in the registry
EPOLL_CTL_MOD: modified event registration on fd
EPOLL_CTL_DEL: delete the event registered on fd

struct epoll_event
{
_uint32_t events;
epoll_data_t data;
};

epoll_data Union
{
void * PTR;
int FD; // the specified event is subordinate descriptors
uint32_t U32;
uint64_t U64;
} epoll_data_t;

- 服务器端处理信号的方式:

void sig_handler(int sig)
{
int save_errno = errno;
int msg = sig;
send(pipefd[1], (char*)&msg, 1, 0);
errno = save_errno;
}

addsig void (int sig)
{
struct sigaction sa;
memset (& a, '\ 0', sizeof (a));
sa.sa_handleer = sig_handler;
sa.sa_flags | = SA_RESTART;
sigfillset (& sa.sa_mask);
assrt (sigaction (sig, & on, NULL)! = -1);
}
`` `

  • Network-related programming signals:
    • SIGHUP : Suspend control terminal process, SIGHUP signal will be triggered.
    • SIGPIPE : Into a closed conduit Socket or write data. The default state is the end of the process.
    • Of SIGURG : band data arrival.
    • SIGCHLD : child process state changes, use the wait for processing
    • SIGTERM : terminate the process. The default kill command transmits the signal.
    • SIGINT : keyboard input to interrupt the process.

Guess you like

Origin www.cnblogs.com/xcb-1024day/p/11332516.html