Program exit caused by sigpipe

Source: Internet
Author: User
After 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.
According to the default signal processing rules, the default operation of the sigpipe signal is terminate (termination, exit), so the client will exit. If you do not want the client to exit, set sigpipe to sig_ign.

For example, signal (sigpipe, sig_ign );
At this time, sigpipe is handed over to the system for processing.

If fork is used on the server, it is necessary to collect the garbage process to prevent the generation of the zombie process. You can handle it like this:
Signal (sigchld, sig_ign); hand it to the system init for recovery.

Here, sub-processes will not generate zombie processes.

Write socketProgramIf you try to send 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 followingCodeTo safely shield sigpipe:
Struct sigaction SA;
SA. sa_handler = sig_ign;
Sigaction (sigpipe, & SA, 0 );

The signal handle set by signal can only be used once. After the signal is captured once, the signal handle will be restored to the default value.
The signal handle set by sigaction can be valid until you change its settings again.

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.