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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 60.0.1(64位)windows版 uploadify使用有问题

    60.0.1(64位)windows版 uploadify使用有问题

    2021年10月26日
    37
  • mt4下载正版官网下载(如何分辨真假MT4软件)

    mt4下载正版官网下载(如何分辨真假MT4软件)在全球零售外汇行业,外汇经纪商使用最多的还是俄罗斯迈达克公司的MT4交易平台,一些不合规的外汇经纪商也对MT4十分热衷,这使市场上几千块一个的盗版MT4日益猖獗,致使一部分交易者因此遭受一些不必要的利益侵害。那么MT4。fOrex6。cc的特点是什么?如何判别一个MT4软件是否是盗版?今天就带你们辨别真假MT4.MT4的优势1.强大的工作表现MT4强大的工作表现,这一点是毋庸置疑的。MT4自2005年7月1日推出以来,就不断的获得市场的认可。下单灵活、界面友好、交易直观等这些都是MT4平台成为外汇市场

    2022年8月15日
    3
  • 什么是贝叶斯

    什么是贝叶斯原文地址[你对贝叶斯统计都有怎样的理解?](https://www.zhihu.com/question/21134457)浅谈贝叶斯不论是学习概率统计还是机器学习的过程中,贝叶斯总是是绕不过去的一道坎,大部分人在学习的时候都是在强行地背公式和套用方法,没有真正去理解其牛逼的思想内涵。我看了一下自己学校里一些涉及到贝叶斯统计的课程,content里的第一条都是PhilosophyofBaye

    2022年5月16日
    40
  • 日期选择器date、week、time、datetime、datetime-local类型

    日期选择器date、week、time、datetime、datetime-local类型

    2021年11月8日
    98
  • 如何查看CentOS操作系统版本「建议收藏」

    如何查看CentOS操作系统版本「建议收藏」1、如何查看已安装的CentOS版本信息:第一种方式:cat/proc/version第二种方式:uname-a第三种方式:uname-rLinuxversion3.10.0-1127.el7.x86_64(mockbuild@kbuilder.bsys.centos.org)(gccversion4.8.520150623(RedHat4.8.5-39)(GCC))#1SMPTueMar3123:36:51UTC20202.、如何查看linu

    2022年6月24日
    44
  • 数据结构与算法Python_数据结构与算法python语言实现

    数据结构与算法Python_数据结构与算法python语言实现我们已经知道算法是具有有限步骤的过程,其最终的目的是为了解决问题,而根据我们的经验,同一个问题的解决方法通常并非唯一。这就产生一个有趣的问题:如何对比用于解决同一问题的不同算法?为了以合理的方式提高程序效率,我们应该知道如何准确评估一个算法的性能。本节学习首先介绍算法分析的重要性,并讲解了分析算法的时间复杂度和空间复杂度分析方法,最后介绍了Python列表和字典常见操作的时间复杂度。

    2022年9月27日
    0

发表回复

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

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