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


相关推荐

  • java des ecb_【转】 java DES ECB模式对称加密解密

    java des ecb_【转】 java DES ECB模式对称加密解密最近需要又要使用DES加密数据,要求DES加密出来的数据为对称加密,经过研究,发现了一些问题:1.DES对称ECB模式加密的数据,长度必须为8的倍数2.加密的数据,加密后先转码(因为加密后的数据我是转码了),否则解密是乱码格式一下是源代码:这个是加密的工具类:packagecom.palmfu.sql;importjava.security.Key;importjavax.crypto.Ciphe…

    2022年10月30日
  • java复习快速导航

    java复习快速导航1.java基础java基础必背知识点java基础加强知识点javaweb1(mysql、HTML、js、xml)javaweb2(tomcat、cookie、el、filter)javaweb3(jquery、ajax、json、redis)maven2.java提高redisdubbo并发JUC阻塞队列、线程池NIOnetty数据库rabbi…………

    2022年7月20日
  • WEB各种环境以及注意事项「建议收藏」

    WEB各种环境以及注意事项「建议收藏」WEB各种环境以及注意事项

    2022年4月22日
  • shell循环执行脚本[通俗易懂]

    shell循环执行脚本[通俗易懂]shell循环执行脚本2017年08月24日15:28:18阅读数:215foriin{1..10} do   echo"Welcome$itimes"   eoo$i.sh done

    2022年7月24日
  • linux redis命令客户端,Redis客户端

    linux redis命令客户端,Redis客户端Redis客户端Redis客户端是一个程序,通过网络连接到Redis服务器,在客户端软件中使用Redis可以识别的命令,向Redis服务器发送命令,告诉Redis想要做什么。Redis把处理结果显示在客户端界面上。通过Redis客户端和Redis服务器交互。Redis客户端发送命令,同时显示Redis服务器的处理结果在Redis命令行客户端redis-cli…

    2022年5月1日
  • iOS layoutSubviews 调用[通俗易懂]

    iOS layoutSubviews 调用[通俗易懂]在初始化时,使用init和-(instancetype)initWithFrame:(CGRect)frame,但是,在调用时是有系统区分的,iOS11.0以下,调用init和-(instancetype)initWithFrame:(CGRect)frame当frame为CGRectZero是不会调用的, 在调用addSubView时,这个跟添加的subView尺寸大小没有关系。 改变…

    2022年7月15日

发表回复

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

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