linux管道、EPIPE 和 SIGPIPE 的关系「建议收藏」

linux管道、EPIPE 和 SIGPIPE 的关系「建议收藏」试验目的:验证试验过程:

大家好,又见面了,我是你们的朋友全栈君。

试验目的:

1、向管道写端写入数据前,关闭管道写端fd,errno值会是什么?

2、向管道写端写入数据后,关闭管道写端fd,从管道读端读取数据时,是否能正常读取数据?

3、向管道写端写入数据后,关闭管道读端fd,从管道读端读取数据时,会发生什么?errno是什么?

4、向管道写端写入输入前,关闭管道读端fd,是否会触发SIGPIPE信号?程序如何不崩溃?errno值是否会为EPIPE?


正常代码流程:

1、创建一个管道pipefd[2]

2、向管道写端pipefd[1]写入数据

3、从管道读端pipefd[0]读取数据

4、正常关闭管道写端和读端


试验结果:

1、errno=8, 写端fd报:Bad file descriptor。不会触发SIGPIPE, errno也不会为EPIPE

2、可以正常读取到写入的数据

3、和1情况一样。errno=8, 读端fd报:Bad file descriptor。不会触发SIGPIPE, errno也不会为EPIPE

4、会触发SIGPIPE。

如果程序不处理SIGPIPE或者按照默认方式处理SIGPIPE,则程序会退出。

如果忽略SIGPIPE( 使用signal(SIGPIPE, SIG_IGN); ),则程序不会因为系统触发SIGPIPE而退出,会继续执行完。

在向管道写端写入数据时,errno=8, 为EPIPE, 报:Broken pipe


结论:

1、程序中忽略 SIGPIPE信号。

2、向管道写端写入数据时,可以检测errno是否为EPIPE,如果是,可以关闭管道写端fd。


代码:

/*
 * pipe_op.c
 *
 *  Created on: Jun 24, 2014
 *      Author: lingyun
 */


#include "pipe_op.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <bits/signum.h>
#include <signal.h>

#define BUFFER_SIZE 1024

void
pipe_create(int pipefd[2]) {

    int ret;

    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, pipefd);
    assert(ret != -1);
}

void
pipe_close(int pipefd) {
    if (pipefd > 0) {
        close(pipefd);
    }
}

void
pipe_func_test() {

    //忽略SIGPIP信号
    signal(SIGPIPE, SIG_IGN);

    //按照默认处理方式处理SIGPIP信号
    //signal(SIGPIPE, SIG_DFL);

    int pipefd[2];

    pipe_create(pipefd);

    int pipe_read_fd = pipefd[0];
    int pipe_write_fd = pipefd[1];

    printf("pipe read end: %d\n", pipe_read_fd);
    printf("pipe write end: %d\n", pipe_write_fd);

    //write msg to write_pipe, and read from read_pipe

    char buf[BUFFER_SIZE];
    memset(buf, '\0', BUFFER_SIZE);
    const char* message = "hello, i'm lingyun";
    int message_len = strlen(message);
    strncpy(buf, message, message_len);

    printf("buf message: %s\n", buf);

    //1. 向管道写端写数据前, 关闭管道写端fd
    //pipe_close(pipe_write_fd);

    //4. 向管道写端写数据前, 关闭管道读端fd
    pipe_close(pipe_read_fd);

    ssize_t writed = 0;
    writed = write(pipe_write_fd, buf, message_len);

    int save_errno;

    if (writed == -1) {
        save_errno = errno;

        if (errno == EPIPE) {
            printf("pipe_write_fd is closed, write to this fd has EPIPE error\n");
        }
        perror("write");
        goto failed;
    }

    printf("want write message len: %d\n", message_len);
    printf("writed %d char\n", writed);

    memset(buf, '\0', BUFFER_SIZE);
    ssize_t readed = 0;

    //2. 向管道写端写完数据后, 关闭管道写端fd
    //pipe_close(pipe_write_fd);

    //3. 向管道写端写完数据后, 从管道读端读取数据前, 关闭管道读端fd
    //pipe_close(pipe_read_fd);

    readed = read(pipe_read_fd, buf, writed);

    if (readed == -1) {
        save_errno = errno;

        if (errno == EPIPE) {
            printf("pipe_write_fd is closed, read from pipe_read_fd has EPIPE error\n");
        }
        perror("read");
        goto failed;
    }

    printf("want read message len: %d\n", writed);
    printf("readed %d char\n", readed);

    printf("after read, buf message: %s\n", buf);

failed:
    if(pipe_read_fd != -1) {
        pipe_close(pipe_read_fd);
    }

    if(pipe_write_fd != -1) {
        pipe_close(pipe_write_fd);
    }
}

int main() {
    pipe_func_test();
    return 0;
}



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/139357.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据结构 || 二维数组按行存储和按列存储[通俗易懂]

    数据结构 || 二维数组按行存储和按列存储[通俗易懂]问题描述:设有数组A[n,m],数组的每个元素长度为3字节,n的值为1~8,m的值为1~10,数组从内存收地址BA开始顺序存放,请分别用列存储方式和行存储方式求A[5,8]的存储首地址为多少。解题说明:(1)为什么要引入以列序为主序和以行序为主序的存储方式?因为一般情况下存储单元是单一的存储结构,而数组可能是多维的结构,则用一维数组存储数组的数据元素就存…

    2022年7月16日
    15
  • 局域网内实现不同网段ip通信_局域网不同网段互访

    局域网内实现不同网段ip通信_局域网不同网段互访1.使用场景电脑使用网段ip为172.23.0.0/16,设备ip为192.168.1.0/24。将电脑和设备通过交换机连接起来,满足了电脑和设备处于同一局域网不同网段,不能进行网络通信。为了能够进行通信,比如,进行设备的密码重置等,都需要能够通信才能完成。2.参考方案可以在电脑的网络设置里的高级配置中,添加一个和设备处于同一网段的ip。需要注意的是,添加的ip之前要先使用ping命令判断局域网中是否存在相同ip的设备,为了避免ip冲突。有时你会发现ping不通的ip,添加之后也有不通的情况。这

    2025年11月1日
    2
  • ubuntu一步架设ftp服务器

    ubuntu一步架设ftp服务器 1.安装sudoapt-getinstallvsftpdsudomkdir/home/myftp sudo/etc/init.d/vsftpdstartsudo/etc/init.d/vsftpdstopsudo/etc/init.d/vsftpdrestart 2.配置sudovi/etc/vsftpd.conf

    2022年7月21日
    10
  • add attribute什么意思_addition的用法及短语

    add attribute什么意思_addition的用法及短语Attributes.Add(“javascript事件”,”javascript语句”);如:this.TextBox1.Attributes.add(“onblue”,”window.Label1.style.backgroundColor=’#000000′;”)

    2022年9月26日
    1
  • robotium例子

    robotium例子android基础知识12:android自动化测试04—Robotium:实例(上):http://daimajishu.iteye.com/blog/1556631robotium方法学习实例:http://blog.csdn.net/gzh0222/article/details/7335666Android自动化测试—Robotium:实例(上):http:/

    2022年7月25日
    8
  • eclipse500错误原因解决方法_eclipse运行无法显示网页

    eclipse500错误原因解决方法_eclipse运行无法显示网页eclipse内部浏览器报错:此错误(HTTP500内部服务器错误)意味着您正在访问的网站出现了服务器问题,此问题阻止了该网页的显示修改eclipse设置即可解决:window-&gt;preferences-&gt;general-&gt;webbrowser选择使用外部浏览器即可IE浏览器设置如下:https://blog.csdn.net/txwtech/article/details…

    2022年8月11日
    9

发表回复

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

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