Program received signal sigpipe, broken pipe

Source: Internet
Author: User

From: http://www.diybl.com/course/3_program/c++/cppjs/20090831/173152.html

I wrote a server program, compiled and executed it in the cygwin environment in windows, and then wrote a multi-threaded client in C # For stress testing. the program has been running normally. but when testing in Linux, it is always inexplicably exited. finally, the log is exited due to a write call. execute the program with GDB and prompt "Broken pipe" when exiting ".

Finally, the problem is determined that a socket that has been closed on the peer end is called twice for write. The second request will generate a sigpipe signal, 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.

From: unpv1
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) generates a sigpipe signal, causing the process to exit.
To prevent the process from exiting, you can capture the sigpipe signal or ignore it and set the sig_ign signal processing function for it:
Signal (sigpipe, sig_ign );
In this way, when the second write method is called,-1 will be returned, and errno is set to sigpipe. The program will know that the Peer has been closed.
PS: in Linux, sigalrm seems to be offset by 1 ms every second. However, in windows, sigalrm is completely tested on time, with no difference of 1 ms.

Method for ignoring sigpipe Signals

Http://hi.baidu.com/greathongjian/blog/item/2f695643091885139213c65a.html

Struct sigaction SA;
SA. sa_handler = sig_ign; // set the action after receiving the specified signal to ignore
SA. sa_flags = 0;
If (sigemptyset (& SA. sa_mask) =-1 | // The initialization signal set is empty.
Sigaction (sigpipe, & SA, 0) =-1) {// shield the sigpipe Signal
Perror ("failed to ignore sigpipe; sigaction ");
Exit (exit_failure );
}

How to block sigpipe exceptions in the pthread
Hi.baidu.com/ailacy/blog/item/a7eb65f8b8b55707d8f9fdd5.html

Http://bbs2.chinaunix.net/viewthread.php? Tid = 985166 & extra = & page = 1

In pthread, you may encounter problems with program converted ed signal sigpipe and broken pipe. The solution is to execute the following code before each thread starts:
# Ifndef Win32
Sigset_t signal_mask;
Sigemptyset (& signal_mask );
Sigaddset (& signal_mask, sigpipe );
Int rc = pthread_sigmask (sig_block, & signal_mask, null );
If (RC! = 0 ){
Printf ("Block sigpipe error/N ");
}
# Endif
Of course, this is only one of multiple methods ~
According to the use experience of Lai banxian, as long as the above Code is written in the main function at the beginning, sigpipe in the pthread thread can be blocked.

[Linux] sigpipe signal and Processing

Http://hi.baidu.com/mckeyzhang/blog/item/d647f26034eee542eaf8f823.html

When writing a socket program in Linux, if you try to send it to a disconnected socket, the underlying layer will throw a sigpipe signal.
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 safely shield sigpipe:
Struct sigaction SA;
SA. sa_handler = sig_ign;
Sigaction (sigpipe, & SA, 0 );
// ================================================ ======================================
Sigpipe
From Wikipedia, the free encyclopedia
Jump to: navigation, search
Sigpipe
Description write on a pipe with no one to read it
Default action Abnormal Termination of the process
Sa_siginfomacros
One
On POSIX-compliant platforms, sigpipe is the signal raised when a computer program attempts to write to apipe without a process connected to the other end. the symbolic constant for sigpipe is defined in the header file signal. h. symbolic signal names are used
Because signal numbers can vary internal SS platforms.
Etymology
SIG is a common prefix for signal names. Pipe refers to the Unix pipe.
Description
UNIX supports the principle of piping, which allows processes to send data to other processes without the need for creating temporary files. when a pipe is broken, the Process Writing to it is sent the sigpipe signal. the default reaction to this signal
A process is to terminate.
A simple example of piping is the following.
Ps l | HEAD
This command, when run on a Unix-like machine (including Linux), returns a list of processes, limited to ten lines.
PS l returns a list of all processes (including those of other users ).
Head selects the first ten lines.
When PS has written ten lines, head has initialized ed all it needs and exits. PS will receive a sigpipe when it tries to write the remaining lines, causing it to terminate as well: it is no use writing data that no one will use. it is also possible that the reading
Process terminates while reading the data. This will also cause sigpipe to be sent to the writing process.
One can ignore sigpipe (using, for example, the signal system call). In this case, all system cballs that wocould cause sigpipe to be sent will return-1 and set errno to epipe.

Broken pipe problem in uken
Www.javaeye.com/topic/437975 #
Some time ago, I encountered the "Alarm Clock" signal problem when processing the latency function (see my "UNIX C latency function summary "). Now we have encountered the "Broken pipe" signal problem during the test, and the program that generates this signal will be suspended.

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.

For the signal generation, we can use the method signal (int signum, sighandler_t handler) to set the signal processing before the signal generation. 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) sig_dfl
2) sig_ign
3) sig_err

In the project, I call signal (sigalrm, sig_ign) and signal (sigpipe, sig_ign). In this way, when sigalam and sigpipe signals are generated, the program will not be aborted and this signal will be ignored directly.

Custom processing method:
C code
Void signal_handle (ing signo)
{
// Do something;
}

Int main ()
{
Signal (sigpipe, signal_handle );
......
}
Disclaimer: The copyright of the javaeye article belongs to the author and is protected by law. You shall not reprint this document without the written consent of the author.

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.