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)
上一篇 2022年5月29日 下午11:46
下一篇 2022年5月30日 上午6:00


相关推荐

  • ETL开发面试题集

    ETL开发面试题集ETL讲解(很详细!!!)ETL是将业务系统的数据经过抽取、清洗转换之后加载到数据仓库的过程,目的是将企业中的分散、零乱、标准不统一的数据整合到一起,为企业的决策提供分析依据。ETL是BI项目重要的一个环节。通常情况下,在BI项目中ETL会花掉整个项目至少1/3的时间,ETL设计…

    2022年6月13日
    77
  • ioctl函数详解_函数concat的作用

    ioctl函数详解_函数concat的作用ioctl函数的作用特殊的read,write,当你用read,write不能完成某一功能时,就用ioctl我这里说的ioctl函数是在驱动程序里的,因为我不知道还有没有别的场合用到了ioctl,所以就规定了我们讨论的范围。为什么要写篇文章呢,是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑。什么是ioctl。ioctl是设备驱动程序中对设备…

    2022年10月18日
    5
  • Handler进阶之sendMessage原理探索

    Handler进阶之sendMessage原理探索Handler 进阶之 sendMessage 本文主要进一步的探索 Handler 主要介绍下 Handler 是如何发送消息的 用过 Handler 的想必对一下几个方法都不会陌生 sendMessage Messagemsg 立刻发送消息 sendMessageA Messagemsg longatTime 在某个时间点发送消息 sendMessageD

    2025年11月3日
    9
  • 超长干货 | Kubernetes命名空间详解

    超长干货 | Kubernetes命名空间详解

    2021年6月30日
    108
  • [分享]在线的代码片段测试工具 jsbin[通俗易懂]

    [分享]在线的代码片段测试工具 jsbin[通俗易懂]有些时候,我们往往有这样的需求:临时测试一个代码片段,不想打开编辑器来新建一个文件,测试完毕又删除想给别人分享一个代码,html文件,css文件,js文件,打个包?向别人展个某个效果,发个文件过去?把代码部署到自己服务器上面?针对这些需求,我们使用在线的代码片段测试工具,也许来得更加简单和方便了。针对前端的在线代码片段工具很多,比较常见的有jsbin和jsfiddle以及codepen.而我最喜欢的就是jsbin了,它有着更多的特性给我带来了极大的方便:任意控制要展示的窗口点击这些标

    2025年7月31日
    7
  • whoosh mysql_使用Whoosh构建自己的搜索引擎

    whoosh mysql_使用Whoosh构建自己的搜索引擎whoosh 是一个纯 python 实现的全文搜索引擎 它小巧轻便 安装简单 设计上参考了 Lucene 性能上虽有欠缺 但贵在方便 无需复杂安装 对于构建小型网站的搜索引擎来说 是一个不错的选择 1 快速入门 whoosh 可以使用 pip 进行安装 pipinstallwh 现在通过官网的例子 快速体验 fromwhoosh indeximportc infromwho

    2026年3月17日
    2

发表回复

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

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