Learn "UNIX Network programming Volume One" Notes __ network communication knowledge

Source: Internet
Author: User
Tags ack mutex

P110 solves three scenarios that network programming may encounter:

(1) When fork a child process, the SIGCHLD signal must be captured; (Understanding: The capture here is not immediately captured, but is registered to capture the signal and is captured whenever the child process sends a signal.) Why to register early is because you do not know when the child process to send. )

(2) When the signal is captured, the interrupted system call must be processed;

(3) The signal processing function of SIGCHLD must be correctly written and the Waitpid function should be used to avoid the zombie process.

P120 solves the problem of passing through socket binary structure:

(1) All the numerical values are passed as text strings, which will not cause the problems of the second-order system and the size-end host in different formats.

(2) Displays the binary format (bits, big end, or small byte order) that defines the supported data types.

P172nagle Algorithm

is to reduce the number of small groupings on the WAN. The algorithm points out that if data is to be confirmed on a given connection, then the act of sending a small packet immediately on the connection that should have been responded to as a user's write action does not occur until the existing data is confirmed.

Often associated with the ACK algorithm: The ACK algorithm allows TCP to receive data after not immediately send ACK, but wait for a period of time (50~200MS), and then send an ACK.

There are three ways to modify the two algorithms that are not appropriate for the use of the above:

(1) using the Writev,writev call eventually results in the invocation of the TCP output function once rather than two times, resulting in a TCP section;

(2) Copy the first 4 bytes of data and the last 396 data to a single buffer, and then call write once for the buffer;

(3) Set the TCP_NODELAY socket option and continue calling write two times. It is not advisable for the method to damage the network.

the difference between P189UDP and TCP

Most TCP servers are a concurrent server, and most UDP servers are iterative servers.

UDP's customer service program is unreliable. If a customer packet is lost, or if a customer packet arrives but the server is unable to answer, the customer will always block in Recvfrom. The way to prevent such persistent blocking is to set a timeout for customer recvfrom.

P270 reentrant and non-reentrant issues

Non-reentrant functions cause the value of global variables to change when invoked between different threads;

A reentrant function call malloc allocating memory can easily cause memory leaks.

P286 Daemon Process

Daemon is a process that runs in the background and is not associated with any control terminal, often from the system initialization script startup.

P316 Avoid using standard I/O libraries on sockets

It is possible to use standard I/O streams on sockets when the convenience of standard I/O streams is greater than the bugs that are caused by buffering, but this is rare.

problems solved by P362 non-blocking accept

When a select returns a listening socket's readable condition, a busy server and client are encountered immediately sending the RST disconnect, causing the server program to block the accept and not handle other already-ready sockets descriptors.

The solution is: (1) When using Select to learn that a listening socket completes the connection preparation to be accept, the listening socket is set to non-blocking;

(2) The following errors, such as Ewouldblock, are ignored in subsequent accept calls.

P366 and network-related IOCTL requests

Socket request, file operation, interface operation, ARP cache operation, routing table operation, stream system.

The P425 signal will be submitted by the kernel anytime and anywhere during program execution, easily blocked in recvfrom, the solution is

Blocking the blocking signal with the Pselect function, it sets the signal mask, the test descriptor and the recovery signal mask three operations that appear to be an atomic operation in the calling process;

The use of sigsetjmp and siglongjmp is equally possible;

Using the IPC from the signal processing function to the master function, the test of the pipe communication jumps out of the for loop to prevent.

the difference between p467udp and TCP, and when to replace TCP with UDP

TCP is reliable, and UDP is unreliable;

In fact, if the application uses broadcast or multicast, it must use UDP;

Establishment and removal of UDP without connection

For real-time audio can be interpolation to make up for lost data, then the missing section may not be necessary retransmission, you can use UDP;

If both ends advance the size of the request and answer agreement, then perhaps no window flow control, you can use UDP;

UDP should not be used for mass data transfer.

Today, a good TCP implementation can give full play to network bandwidth capacity, and more and more applications are designed to recreate a TCP in UDP.

P534 The network programming differences between process and thread calls

Process fork to copy the memory image of the parent process to the child process;

IPC mode is used for communication between parent-child processes;

Threads are light weight processes that create faster;

It is easy to share global memory between threads, but you need to consider synchronization issues.

p674 client/server programming paradigm

Iterative server, no process;

Concurrent server, each client requests fork a child process;

Pre-derivation of child processes, with no protected invocation of each child process accept;

Pre-derivation of child processes, using file locks to protect accept;

Pre-derivation of child processes, using thread mutex lock to protect accept;

In advance, the parent process passes the socket descriptor to the child process;

Concurrent server, each client requests to create a thread;

Create a thread server in advance, protect accept with a mutex lock;

Create a thread server in advance, called accept by the main thread.

Comparison of p674 client/server programming paradigm

When the load is lighter, the concurrent server can be selected;

Create a child process pool or thread pool faster than a concurrent server;

It is quicker for all child processes or threads to invoke accept alone than to let the parent process or the main thread call accept and then pass the descriptor;

It is preferable to block all child processes or threads in the same accept than to block in the same single select;

Using threads is faster than a process, but it also depends on the specific operating system.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.