Chapter 5 No blocking socket and single process polling server
· 5.1 No blocked socket
The clogging socket will enter sleep when waiting for input / output, and cannot continue other operations. This disadvantage is not obvious in concurrent server mode, but in some complex applications may need multiple connection services in single processes, and the blocking socket will greatly reduce efficiency. In addition, the process may have been blocked. For example, the server is collapsed, and the client does not know, the client process will have been blocking.
No blocking sockets will affect read, write, establish a connection, and receive the connection process. In general, it is not waiting for all resources to pass, and then operates and returns it, this is some distinct from the process without blocked sockets. If this has been blocked and since there is no blocked socket, then Errno will return ewouldblock, which can be judged by the following statement:
Ret = accept (...);
IF (RET <0 & Errno! = EWouldBlock) // If the error returns and the reason is not no blocked
There are two implementations of no clogging socket:
int (Flags = Fcntl (SOCK_FD, F_GETFL, 0) <0) Error_Proc ();
Flags | = o_nonblock;
IF (FCNTL (SOCK_FD, F_SETFL, FLAGS) <0) Error_Proc (); // This method is a POSIX standard definition method
INT b_on = 1; // Use fionbio command in the ocl function
IOCTL (SOCK_FD, Fionbio, & B_ON);
· 5.2 single process polling server mode
Make_null (serv_slot, maxlen); // serv_slot [] is a connection socket descriptor array, this process provides services
Listen (listen_fd, maxsize); // Establish a listening socket
Do {
// Receive a connection socket descriptor from the fully listening queue
CONN_FD = Accept (Listen_FD, (Struct SockAddr *) & cli_addr, sizeof (cli_addr));
IF (conn_fd <0 && errno! = ewouldblock)
Error_Proc (); // Error handling
ELSE IF (CONN_FD> = 0) // Receive a new connection socket descriptor
CREATE_NEW_CONNECT (conn_fd, serv_slot, & maxlen); // Establish a new connection
For (i = 0; i Serve_for (serv_slot [i]); // This process is the i-th connection service WHILE (Continue); Using a single process polling server mode still cannot avoid certain accidents (such as non-normal disconnections) or malicious behavior, and if the number of customers increases, the corresponding delay of the server is also increased. So we still use concurrent server mode to provide parallel services because a server sub-process failure does not affect the work of other processes. Chapter 6 with external data and multiplexing, signal driver input / output model · 6.1 multiplexed input / output model The concept of multiplexing: The process is not an active inquiry socket situation, but it is desirable to register with the monitoring of the monitor, and then the passive attitude is waiting. When an event occurs on the monitoring socket, the process checks the situation that the occurrence and then do the corresponding processing. In this way of work, the process is known to detect an event on the socket, and enter the sleep state when there is no event. Header file: INT SELECT (int Maxfd, fd_set * rdset, fd_set * wrset, fdset * exset, struct timeval * timeout); [MAXFD is the maximum file descriptor value 1 that needs to be monitored, that is, the system monitors the file descriptor from 0 to MAXFD-1; RDSET, WRSET, EXSET is a collection of readable, writable and exception file descriptors that require detection; There is no event in Timeout, the function returns 0] INT Pselect (int Maxfd, fd_set * rdset, fd_set * wrset, fdset * exset, struct timeespec * timeout, const numSt_t sigmask; // Posix enhancements to the SELECT function, parameter sigmask is executed after the blocking signal recovery Document descriptor collection: FD_ZERO (fd_set * fdset); // Clear initialization FD_SET (int FD, fd_set * fdset); // increase FD_CLR (int FD, FD_SET * fdset); // Delete FD_ISSET (int FD, fd_set * fdset); // Judgment contains · Read, write and abnormal ready conditions Reading: Listening to the tangle full connection queue establishes a new connection; the read buffer of the connecting socket exceeds the reading limit, the read conduit shutdown and the socket exception. Write ready: The write buffer of the connection socket is idle less than a lower limit, the write pipe is closed and the socket is abnormal. Abnormal Readiness: The tape data is reached on the socket. Exception is connected to read, write ready. Some common ready conditions are listed above, and the specific reference help manual. Basic usage: FD_ZERO (& r_set); // Initialization Fd_set (listen_fd, & r_set); // Add a readable file descriptor collection RET = SELECT (Listen_FD 1, & r_set, null, null, null); // Ready for listening sockets · 6.2 Signal driven input / output model Signal drives are typically used to receive emergency data. The process is registered to the system, and then the system detects that the data arrives will occur to the recipient, and the recipient receives data in the signal processor. This approach is usually used in receiving an emergency control data. Data recipient setting: #include INT FCNTL (int FD, INT CMD, ...); // Use the command f_setown, the third parameter If the integer represents the process number, the negative integer represents the process group receive · Summary of 6.3 System I / O Models This book tells the four I / O models of "clogging method, non-clogging method, multiplexing and signal driver". 1 clogging method: Widely used on the concurrent server, when the socket does not satisfy the operating conditions, block the waiting resource immediately. 2 non-blocking method: Widely used on single-process polling servers, waste larger CPU resource is used. 3 multiplexing methods: Widely used in multi-client services in a single process, saving CPUs in polling than non-clogging methods. 4 Signal drive mode: Widely used in receiving emergency data. · 6.4 Receiving and sending external data The external data refers to data transmitted outside the normal data stream channel, which is usually used in synchronization and control of the remote process. It is almost the same as the signal drive mode, but the SIGURG signal is sent, not SIGIO. Header file: INT Send (int LEN, INT FLAGS); // Send external data using the MSG_OOB Control Options INT RECV (Int Sockfd, Void * BUF, INT LEN, INT FLAGS); // Receive external data using the MSG_oob control option External data allows only one byte, such as Send (SOCK_FD, "BC", 2, MSG_OOB), TCP only believes that the last one is an outgoing data, which is previously ordinary data. In special cases, the out-of-band packet is preferred by the recipient. The recipient is used by default (using the IOCTL function to change) to receive strip data using a byte of the outer strip data buffer, and the outer strip data segment and the normal socket data segment character set can distinguish the outer data. And ordinary data. If the recipient receives multiple out-of-band data segments, TCPs will compare data in data segments previously received, if their values are the same, they think they are the same extracted data segment. Since the sender may send multiple outer strip data segments, the receiving strip data must be fault tolerant. The receiver only allows a byte of outer data, and the first-served data segment will overwrite any later. The external data is covered and become normal data. Received tape data will trigger an exception. The abnormality is released until the read pointer is greater than the external data indication (emergency) pointer. The server-side receiving strip data can be used: 1 multiplexing mode, points: Detecting abnormal ready and reading ready-to-chronological order is different, and the results are different. 2 Asynchronous signal driver, points: Design a SIGURG signal processor, pay attention to the shield / blocking signal before and after processing. 3 Detect the external data tag method, points: // Socket is set to SO_OOBINLINE, the outer data is seen as normal data storage, ON = 1 Setsockopt (CONN_FD, SOL_SOCKET, SO_OOBINLINE, & ON, SIZEOF (ON)); IOCTRL (conn_fd, siocatmark, & n_data); // detects whether the read pointer is combined with the external data indicator pointer IF (n_data == 1) // external data arrives Note: The covered out-of-band data will remain in the read buffer, followed by an ordinary data. If the external data used is not deleted in time, the extracted data may be read in a normal data on the spot, such as the Sleep () system call may result in such a process that requires emergency processes to produce a strange behavior!