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


相关推荐

  • Solaris 10网络服务

    Solaris 10网络服务

    2021年7月23日
    86
  • 解决:VUE同一路由强制刷新页面

    解决:VUE同一路由强制刷新页面解决:VUE同一路由强制刷新页面

    2022年7月11日
    69
  • 5种很牛的两位数乘法速算方法「建议收藏」

    5种很牛的两位数乘法速算方法「建议收藏」5种很牛的两位数乘法速算方法第一种:十几乘以任意两位数公式推导(10+b)(10c+d)=100c+10(bc+d)+bd=10(10c+bc+d)+bd=10((10c+d)+bc)+bd 方法:如果c=1,那么就是一个因数,加上另一个因数的尾数,做前积;两个因数的乘积,做尾积应用举例:11×11=|11+1||1×1|=12112×14=|12+4||2×4|=1681

    2022年6月8日
    56
  • javah详解[通俗易懂]

    javah详解[通俗易懂]java开发中如果使用到JNI,则难免需要使用javah来生成C++或C的头文件信息,下面就讲解javah的命令:第一种:直接cd到当前程序的target/class目录下(一定不能是子目录)(maven项目,如果是普通项目则到bin目录下)。然后使用:javahcom.yongcheng.liuyang.utils.TestJni,其中javah后面的是需要生成头文件类的全路径(包名+类名),当然生成的.h文件位于当前class的目录下。第二种:直接在运行中cmd到dos窗口,使用如下命令:j

    2022年9月25日
    2
  • DS18B20温度传感器的工作原理_基于ds18b20的温度报警仿真

    DS18B20温度传感器的工作原理_基于ds18b20的温度报警仿真数字温度传感器(DS18B20)传感器参数

    2022年9月17日
    1
  • CentOS安装MySQL8.0「建议收藏」

    CentOS安装MySQL8.0「建议收藏」目录配置yum源wget下载源安装包安装源安装MySQL启动相关启动服务查看启动状态设置开机自启重启服务配置修改密码卸载卸载MySQL删除文件配置yum源wget下载源安装包如果没有安装wget,先安装yum-yinstallwget进入mysql官网下载:https://dev.mysql.com/downloads/repo/yum/复制下载链接:https://dev.mysql.com/get/mysql80-community-release-el7-4.noarch.rp

    2022年6月26日
    25

发表回复

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

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