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


相关推荐

  • 通俗易懂教你画类图

    通俗易懂教你画类图1.类图1.1类的内容第一层:类的名称,如果是抽象类,则就用斜体显示。第二层:字段和属性。第三层:方法。前面的符号:“+”表示public;”-“表示private;”#”表示protected。1.2类与类之间的关系1.2.1继承关系:空心三角形+实线鸟也有生命这种属性,也有新陈代谢和繁殖的行为。1.2.2实现关系:空心三角形+虚线大雁可以各种各样的飞翔方式。1.2.3关联关系:实线箭头企鹅需要知道气候的变化,需要了解气候规律。1.2.

    2022年7月12日
    25
  • 计算机基础

    计算机基础

    2021年5月20日
    126
  • SaveFileDialog无法打开

    SaveFileDialog无法打开在线程中SaveFileDialog无法打开。Threadt=newThread(saveDialog);t.IsBackground=true;t.SetApartmentState(ApartmentState.STA);//加上这句就可以了t.Start();当前画面是报表画面,没用线程。主画面点击按钮,打开该画面,是在子线程中打开的,所以SaveFileDialog也无法直接打开…

    2022年10月8日
    3
  • android之OkHttpClient通信「建议收藏」

    android之OkHttpClient通信OkHttpClient用法1:自定义缓存OkHttpClienthttpclient=newOkHttpClient.Builder().cache(newCache(newFile("cacheDirectory"),newLong(10*1024*1024))).build();用法2:当…

    2022年4月17日
    315
  • 深度图转换成点云[通俗易懂]

    深度图转换成点云[通俗易懂]一、概述最近由于课题需要数据源,但是没有直接获取的方法,所以只能在周老师http://www.qianyi.info/的网站上自己下载深度图转换成点云数据,大概花了三点的时间,终于弄得差不多了,这里做个记录。二、数据准备和环境配置1、数据下载在http://redwood-data.org/indoor/dataset.html上下载CleanDepthSequence和Groun…

    2022年5月30日
    47
  • checklistbox控件用法总结

    checklistbox控件用法总结一般认为:foreach(objectobjincheckedListBox1.SelectedItems)即可遍历选中的值。其实这里遍历的只是高亮的值并不是打勾的值。遍历打勾的值要用下面的代码:for (int i = 0; i {    if (checkedListBox1.GetItemChecked(i))    {        MessageBox

    2022年6月16日
    33

发表回复

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

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