linux sigpipe信号,Linux下SIGPIPE信号及其处理「建议收藏」
大家好,又见面了,我是你们的朋友全栈君。
在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.
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/139327.html原文链接:https://javaforall.cn
相关文章
- linux网络配置出现E325,Linux CentOS E325错误,如何解决?VI如何使用?「建议收藏」
- Linux系统:用户密码过期处理指南(linux用户密码过期)
- 文件Linux查找不存在文件的方法(linux判断不存在)
- Linux下删除目录的指令(删除目录命令linux)
- Linux开发工程师:有高薪,有充足待遇(linux开发工程师待遇)
- 『Linux下快速有效的报文处理技术』(linux报文处理)
- 学习的重要性Linux学习:拥抱未来的必要性(《linux)
- Linux: 无视大小写自由探索(让linux不区分大小写)
- Linux环境下的文件压缩处理(linux下的压缩文件)
- 处理Linux下的图片水印处理简易指南(linux图片水印)
- 申请Linux系统中断处理流程 .(linux中断申请)
- 处理Linux内存管理的日志记录(linux内存日志)
- Linux空格处理:转义之道(linux空格转义)
- 处理Linux下Flash存储技术的应用(linux闪存)
- 据处理Linux网络连接数据高效处理(linux网络连接数)
- 深入了解Linux系统中断处理流程(linux中断流程)
- 中处理json使用Linux脚本处理JSON格式数据(在linux脚本)
- Linux快速上传/下载文件的方法(linux上传下载文件)
- Linux设备驱动:中断处理策略(linux设备驱动中断)
- 文件Linux快速查询子目录中的文件(linux查询子目录)
- Linux操作系统之多元分支(linux的分支)
- Linux 中查看文件命令:ls(linux显示文件命令)
- 驱动配置Linux挂载:网卡驱动安装与配置(linux挂载网卡)
- 了解Linux系统的基础知识和操作技巧(了linux)
- Linux多网卡配置指南,让你轻松处理网络连接(linux多网卡配置)
- 了解Linux下GBK编码的语言处理(linux语言gbk)
- Linux实用指令:使用f选项获取更详细的文件信息(linux-f)
- 快速入门:ESXi虚拟机安装Linux系统步骤(esxi安装linux)
- Linux读写同步:保障数据一致性(linux读写同步)
- Linux 异常处理:从捕获到应对(linux异常捕获)
- 【Linux初学者必备】25个提高学习效率的建议(linux学习建议)