Errno
In unix systems, when an exception is returned for most system calls, the usual return value is-1, and the global variable errno (errno. h), such as socket (), bind (), accept (), listen (). Erron stores a positive integer to save the error value of the previous error.
For a thread, each thread has a dedicated errno variable, so you do not have to consider synchronization.
Strerror converts to English (Note: use strerror_r for thread safety)
Perror is simplified strerror/fprintf
Slow system call
System calls that may be permanently blocked and cannot be returned, usually some read/write examples, such as pipe, terminal devices, network connections, typical accept (), read (), write (), open (), select (), epoll (), etc. The basic rule for slow system calls is: when a process blocked by a slow System Call captures a signal and the corresponding signal processing function returns, the system call may return an EINTR error, set the errno value to this value. Although some kernels will restart the system, from the perspective of portability, EINTR processing is required. Note that this cannot be done for connect. (UNP 5.9)
A signal is responded by a signal processing function. during processing, the signal is blocked. The standard signal implementation has no queuing function, so the signal may be lost, and multiple consecutive signals cannot be processed. It is a good solution to use the waidpid () enumeration check. See UNP 5.10.
About TOE (TCP offload engine)
A small detail of local packet capture:
Checksum incorrect appears. It is caused by the TOE engine on the NIC. TOE wiki
Using the TCP/IP protocol to process network traffic requires a large amount of server resources. To reduce the pressure on the server, a technology called TCP Offload Engine: TOE came into being. The TCP burden reduction engine is generally composed of two components: hardware and software. It extends the functions of the traditional TIP/IP protocol stack, transfer all the processing of network data traffic to the integrated hardware on the NIC. The server only processes TCP/IP control information.
Generally, the TCP/IP protocol stack of the operating system performs the calculation of TCP/UDP/IP checksum. The function of integrating TOE into the NIC includes the calculation of checksum. After the Rx Checksum Offload/Tx Checksum Offload is set to Enable, the protocol stack does not perform Checksum calculation, but is completed by the NIC itself.
You can modify the attributes of the NIC to avoid checksum incorrect and disable Checksum Offload. Checksum incorrect can be corrected at the cost of reducing network performance.
This checksum does not seem to have any impact on application-layer programs... (Personal feeling)
I/O processing on Socket
The standard I/O Library mentioned above (standard I/O summary) is a set of advanced Input and Output Functions Defined by ansi c, which is easier than simply using UNIX system I/O.
However, the standard I/O Library does not provide a way to read file metadata and is not suitable for processing special files such as socket.
Insufficient value (short count)
Definition. In short, if I want to process 10 bytes and only 6 are processed, the value below is 6. Note not 4
Because of the limited buffer size and network latency of the nested interfaces in the kernel, read and write actually process fewer bytes than expected. Other problems such as EOF or reading from a terminal may also cause insufficient values.
When reading and writing disk files, there will be no insufficient values
Processing Method:
Both UNP 3.9 and CSAPP 10.9 (RIO) provide solutions. For reference, the following code:
int simon_send(int fd, char* buf, unsigned int n){int left = n;char *bufptr = buf;int send_bytes;while (left >= 0){if ((send_bytes = send(fd, bufptr, MAX_BUF_SIZE, 0)) < 0) {if (errno == EINTR) //iterrupted by signal , send againsend_bytes = 0;elsebreak;}else if (!send_bytes) // EOF or socket shutdown by peerbreak;left -= send_bytes;bufptr += send_bytes;}return n - left;}int simon_recv(int fd, char* buf, unsigned int n){int left = n;char *bufptr = buf;int recv_bytes;while (left >= 0){if ((recv_bytes = recv(fd, bufptr, MAX_BUF_SIZE, 0)) < 0){if (errno == EINTR) recv_bytes = 0;elsereturn -1;}else if (!recv_bytes) break;left -= recv_bytes;bufptr += recv_bytes;}return n - left;}In addition, the use of the recv with the flag MSG_WAITALL can also be processed to a certain extent.
For the selection and comparison of I/O, see (from a ppt ):