search

Linux SIGPIPE signal

Feb 19, 2024 pm 04:00 PM
tcpchannelsigpipe

Among the TCP communication parties, for the convenience of description, the communication parties are replaced by A and B below.

According to the TCP protocol, if B continues to send data after A closes the connection, B will receive A's RST response. If B continues to send data, the system will send a SIGPIPE signal to inform that the connection has been disconnected and stop sending.

The system's default processing behavior for the SIGPIPE signal is to let process B exit.

The default processing behavior of the operating system for the SIGPIPE signal is very unfriendly, let us analyze it.

Linux SIGPIPE信号

TCP communication is a full-duplex channel, which is equivalent to two simplex channels, and each end of the connection is responsible for one.

When the peer "closes", although the intention is to close the entire two channels, the local end only receives the FIN packet.

According to the provisions of the TCP protocol, when one end closes the one-way channel it is responsible for, it can still receive data but no longer send data.

In other words, due to the limitations of the TCP protocol, the communicating party cannot know whether the peer's socket has called close or shutdown.

int shutdown(int socket, int how);

The parameter "how" of the shutdown function can be set to close SHUT_RD, SHUT_WR or SHUT_RDWR, which is used to indicate closing the receiving and sending individual channels or closing the sending and receiving channels at the same time.

Call the read/recv method on a socket that has received a FIN packet. If the receiving buffer is empty, 0 is returned, which is often said to indicate that the connection is closed. However, when the write/send method is called for the first time, if there is no problem with the sending buffer, correct writing will be returned (that is, the return value of the write/send function is greater than 0), but the message sent will cause the peer to respond with an RST message. Because the last time the program called write/send, it was normal. When trying to call the write/send function again, the SIGPIPE signal was generated, causing the process to exit.

This default behavior is for us to develop programs, especially for back-end services, which need to serve many clients at the same time. The entire process cannot exit and cannot continue to serve other clients because there is a problem with the connection to a certain client. .

In order to avoid this phenomenon, you can capture the SIGPIPE signal and process it or ignore the signal. The code for ignoring the signal is as follows:

signal(SIGPIPE, SIG_IGN);

After this setting, when the write/send method is called for the second time, -1 will be returned, and the errno error code will be set to SIGPIPE, so the program will know that the peer has been closed.

The above is the detailed content of Linux SIGPIPE signal. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:每日运维. If there is any infringement, please contact admin@php.cn delete
How to Fix Attachment Not Showing in Outlook 365?How to Fix Attachment Not Showing in Outlook 365?Apr 19, 2025 am 12:50 AM

Do you have trouble downloading or sending attachments in Outlook 365? Sometimes, Outlook doesn’t show them for some unknown reason, so you are unable to see them. In this post on php.cn Website, we collect some use tips for attachments not showing.

How to Fix V Rising Connection Timed out? Here Are 5 Solutions! - MiniToolHow to Fix V Rising Connection Timed out? Here Are 5 Solutions! - MiniToolApr 19, 2025 am 12:49 AM

When V Rising players try to join a server that is close to or already full, they may encounter the “V Rising connection timed out” issue. If you are one of them, you can refer to this post from php.cn to get solutions. Now, keep on your reading.

How to Turn on/off Real-Time Protection in Windows Security? - MiniToolHow to Turn on/off Real-Time Protection in Windows Security? - MiniToolApr 19, 2025 am 12:48 AM

Windows supplies real-time protection via Windows Security. But this feature may prevent you from doing something it thinks are dangerous. In this situation, you may want to temporarily turn on real-time protection. This php.cn post will show you how

Windows 11 24H2 or Windows 12? Does This Matter?Windows 11 24H2 or Windows 12? Does This Matter?Apr 19, 2025 am 12:47 AM

Microsoft has started working on next year’s Windows updates very early. Recent rumors state that the next update in 2024 might be Windows 11 24H2 rather than Windows 12. Everything is uncertain now. php.cn will now take you to see some related infor

Fix: Unable to Perform Requested Operation – Error 0x80030001 - MiniToolFix: Unable to Perform Requested Operation – Error 0x80030001 - MiniToolApr 19, 2025 am 12:46 AM

The error 0x80030001 often happens when you are attempting to copy files. The error code will be accompanied by a message that tells “unable to perform requested operation”. If you are struggling with this error, you can read this article on php.cn W

How to Download and Install Windows 11 KB5034765How to Download and Install Windows 11 KB5034765Apr 19, 2025 am 12:45 AM

On February 13, 2024, Microsoft released KB5034765 (OS builds 22621.3155 and 22631.3155) for Windows 11 22H2 and Windows 11 23H2. This security update brings you many new improvements and bug fixes. You can learn how to download and install Windows 1

Power Management Tab Missing from Device Manager - Top GuidePower Management Tab Missing from Device Manager - Top GuideApr 19, 2025 am 12:44 AM

Device Manager is widely used when you need to fix some computer issues. You can check the problematic devices and decide to uninstall or update device drivers. Besides, you can also set Power Management settings in Device Manager. However, you may f

2 Ways to Reset Windows Backup to Default in Windows 11/102 Ways to Reset Windows Backup to Default in Windows 11/10Apr 19, 2025 am 12:43 AM

When Backup and Restore (Windows Backup) fails to work, you can choose to reset it to default. How to restore Windows Backup to default in Windows 11/10? php.cn will guide you to easily do this thing in 2 ways and let’s go to see them.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.