Solution to the broken pipe error in Linux

Source: Internet
Author: User

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.

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 signal generation, we can use the method signal (int signum, sighandler_t handler) before signal generation)
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 ). We can call the system's processing method or
To customize the processing method.

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 the write method is called for the first time, if there is no problem in sending buffering,
Will return the correct write (send). But the sent packet will cause the Peer to send the RST packet,
Because the socket at the peer end has 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,
Set the sig_ign signal processing function for it:

Signal (sigpipe, sig_ign );

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

PS: in Linux, sigalrm seems to be offset by 1 ms every one second,
However, Windows is completely tested on time, with no difference of 1 ms.

Header file # include <signal. h>

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

In pthread, you may encounter program encoded ed signal sigpipe, broken
To solve the pipe problem, run 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 ");
}

 

 

 

 

Linux Signal Set -- sigset_t typedef struct used to describe the signal {
Unsigned long sig [_ nsig_words];
} Sigset_t

 

 

Header file and signal processing function:

Header file
# Include <signal. h>
Sigemptyset (sigset_t * Set) initializes the signal set specified by the set, and all signals in the signal set are cleared;
After sigfillset (sigset_t * Set) calls this function, the signal set points to will contain 64 types of signals supported by Linux;
Sigaddset (sigset_t * Set, int SIGNUM) adds the SIGNUM signal to the signal set pointed to by the Set;
Sigdelset (sigset_t * Set, int SIGNUM) deletes the SIGNUM signal in the signal set pointed to by the Set;
Sigismember (const sigset_t * Set, int SIGNUM) determines whether the signal SIGNUM is in the signal set pointed to by the set

 

 

Note: This method does not seem to work!

 

 

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.