linux sigpipe信号,Linux下SIGPIPE信号及其处理「建议收藏」

linux sigpipe信号,Linux下SIGPIPE信号及其处理「建议收藏」在Linux下写socket的程序的时候,如果尝试send到一个disconnectedsocket上,就会让底层抛出一个SIGPIPE信号。这个信号的缺省处理方法是退出进程,大多数时候这都不是我们期望的。因此我们需要重载这个信号的处理方法。调用以下代码,即可安全的屏蔽SIGPIPE:structsigactionsa;sa.sa_handler=SIG_IGN;sigaction(S…

大家好,又见面了,我是你们的朋友全栈君。

在Linux下写socket的程序的时候,如果尝试send到一个disconnected socket上,就会让底层抛出一个SIGPIPE信号。

这个信号的缺省处理方法是退出进程,大多数时候这都不是我们期望的。因此我们需要重载这个信号的处理方法。

调用以下代码,即可安全的屏蔽SIGPIPE:

struct sigaction sa;

sa.sa_handler = SIG_IGN;

sigaction( SIGPIPE, &sa, 0 );

//======================================================================

SIGPIPE

From Wikipedia, the free encyclopedia

Jump to: navigation, searchSIGPIPE 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 a pipe 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 across 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 for 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 received 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 calls that would cause SIGPIPE to be sent will return -1 and set errno to EPIPE.0b1331709591d260c1c78e86d0c51c18.png

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/139327.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • clion 2022.01.13 激活码【最新永久激活】

    (clion 2022.01.13 激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlLGWSVFD4PZ-eyJsaWN…

    2022年4月1日
    205
  • CMS相关技术实现

    CMS相关技术实现最近要实现单位的CMS(内容管理系统)遇到一个富文本编辑的问题,前台框架是jsp+spring;后台框架打算用flex+spring,不知道这样的框架是否可行?1.不知道这样的框架是否可行?2.由于flex还不能很好的支持富文本编辑(要求的是能够对word,excel进行可视化操作的富文本),打算用IFRAME框架集成ewebeditor,不知道这种方案是否可行…

    2022年6月1日
    28
  • vector的find用法[通俗易懂]

    vector的find用法[通俗易懂]一.find函数存在于算法中其头文件为#include<algorithm>二.代码示例:#include<vector>#include<algorithm>#include<iostream>usingnamespacestd;intmain(){vector<int>L;L.pu…

    2022年10月10日
    0
  • 天猫送给这些商家“首页置顶”丨 24张PPT详解天猫消费电子2018年商家策略

    天猫送给这些商家“首页置顶”丨 24张PPT详解天猫消费电子2018年商家策略

    2022年4月2日
    57
  • LaTeX 插入图片失败[通俗易懂]

    LaTeX 插入图片失败[通俗易懂]LaTeX插入图片失败(引擎pdflatex)\includegraphics使用figure环境本身可正常编译,只使用\centering时\caption可正常插入。增加\includegraphics[width=10cm]{..}插入png图片时,报错100行,第101行报“Toomanyerrors.TeXstopped.”(已\usepackage{graphicx},不是旧包graphics的问题)几种报错语句重复出现,可能是循环错。“Missinge

    2022年6月6日
    39
  • 2021.7goland激活码【2021.7最新】

    (2021.7goland激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月21日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号