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


相关推荐

  • LevelDB库功能详解

    LevelDB库简介  一、LevelDB入门LevelDB是Google开源的持久化KV单机数据库,具有很高的随机写,顺序读/写性能,但是随机读的性能很一般,也就是说,LevelDB很适合应用在查询较少,而写很多的场景。LevelDB应用了LSM(LogStructuredMerge)策略,lsm_tree对索引变更进行延迟及批量处理,并通过一种类似于归并

    2022年4月8日
    40
  • 微信小程序php签到功能,微信小程序签到功能

    微信小程序php签到功能,微信小程序签到功能本文实例为大家分享了简易微信小程序签到功能的具体代码 供大家参考 具体内容如下一 效果图点击签到后二 数据库用一张数据表存用户签到的信息 每次用户签到都会往表中添加一条记录了用户 id 和签到日期的数据 如下图三 后端后端写两个接口 一个用于查询用户今日是否签到和签到记录总数 一个用于添加用户签到信息到数据库 这里用的是 python 的 flask 框架 1 查询用户签到信息接口 app route

    2026年3月18日
    2
  • webstorm2021激活码_通用破解码

    webstorm2021激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    278
  • 阿里 Blink 正式开源,重要优化点解读

    阿里 Blink 正式开源,重要优化点解读

    2021年6月29日
    102
  • Hello-Agents 第十章 智能体通信协议

    Hello-Agents 第十章 智能体通信协议

    2026年3月15日
    2
  • Android获取短信验证码并自动填充的两种方式「建议收藏」

    Android获取短信验证码并自动填充的两种方式「建议收藏」有些项目为了方便客户操作,减去客户输入短信验证码的时间,会要求安卓app能够获取收到的短信验证码并自动填充到输入框。所以,我整理了安卓获取短信验证码并自动填充输入框的两种方法,而且正式在项目中使用并无bug。      一:监听短信库的变化,获取收到的短信内容,并通过正则表达式取出数字验证码填充到输入框中。代码如下:      publicclassObtainSmsVerifyC

    2022年7月25日
    90

发表回复

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

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