Sigpipe signal details

Source: Internet
Author: User

Sigpipe signal details

When the server closes a connection, if the client sends data. According to the TCP protocol, an rst response is received. When the client sends data to the server, the system sends a sigpipe signal to the process, telling the process that the connection has been disconnected, do not write any more.

I wrote a server programLinuxThen, the client is written in C ++ to perform stress testing with tens of millions of short links. However, the server is always inexplicably exited without core files.

Finally, the problem is determined to call two write operations on a socket that has been closed on the peer end. The second write operation will generateSigpipeSignal, which ends the process by default.

The specific analysis can be combined with TCP's "Four handshakes" to close. TCP is a full-duplex channel. It can be seen as two ticket channels. The two ends of the TCP connection are each responsible for one. when the peer calls close, although the intention is to close the entire two channels, the local end only receives the fin package. according to the semantics of the TCP protocol, the peer only closes the ticket channel in charge of it and can continue to receive data. that is to say, due to the restrictions of the TCP protocol, an endpoint cannot know whether the socket of the Peer side calls close or shutdown.

Call the read method for a socket that has received the FIN packet. If the receiving buffer is empty, 0 is returned. This is often said to indicate that the connection is closed. but when you call the write Method for it for the first time, if there is no problem with the sending buffer, the correct write (sending) will be returned ). however, the sent packets will cause the Peer to send the RST packet, because the socket at the peer end has already called close and is completely closed, neither sending nor receiving data. therefore, the second call to the write method (assuming that after receiving the RST) will generateSigpipeSignal, causing the process to exit.

To avoid process exit, captureSigpipeSignal, or ignore it and set itSig_ignSignal processing functions:

Signal(Sigpipe,Sig_ign);

In this way,-1 is returned when the second write method is called, and errno is setSigpipeThe program will be able to know that the Peer has been closed.


InLinuxWhen writing a socket program, if you try to send it to a disconnected socket, the underlying layer will throwSigpipeSignal.
The default processing method for this signal is to exit the process, which is not what we expect most of the time. Therefore, we need to reload the processing method of this signal. Call the following code to secure blocking.Sigpipe:

Signal (sigpipe, sig_ign );

The reason why my program generates this signal is:
After the client sends a message to the server through pipe, it closes the client. At this time, the server generates a broken pipe signal when it returns the message to the client, the server will be ended by the system.


We can use this method before generating a signal.Signal(Int signum, sighandler_t handler) set signal processing. If this method is not called, the system will call the default processing method: Abort the program and display a prompt (that is, we often encounter problems ). You can call the system's processing methods or customize the processing methods.

Three processing methods are defined in the system:
(1) default actions for sig_dfl signals:
(A) if the default action is to suspend a thread, the thread's execution is temporarily suspended. When the thread is paused, no additional signals sent to the thread are delivered until the thread starts executing, except for sigkill.
(B) Set the signal action of the suspended signal to sig_dfl, and the default action is to ignore the signal (sigchld ).
(2) sig_ign ignores Signals
(A) The delivery of the signal has no effect on the thread.
(B) The system does not allow the operation of the sigkill or sigtop signal to be set to sig_dfl.
3) sig_err

Project I calledSignal(Sigpipe,Sig_ign).SigpipeWhen the signal is sent, the program will not be aborted, and the signal will be ignored directly.

Sigpipe signal details

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.