使用信号和SIGPIPE

使用信号和SIGPIPE

本文介绍了使用信号和SIGPIPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,涉及编写程序用fork(进程)来处理数据(计算圆周率)的分配,信号和选择。

I'm working on an assignment that involves writing a program to process data (calculate pi) using fork (processes), signals and select.

我现在正在正确的信号,我认为我想要做的是,所以如果程序捕获它,它会尝试再次写入管道(如果一个进程试图写入到一个管道使用该SIGPIPE还没有阅读器,它将被发送的SIGPIPE)

I'm working right now on the signals and what I think I want to do is to use SIGPIPE so if the programs catches it, it tries to write to the pipe again (If a process tries to write to a pipe that has no reader, it will be sent the SIGPIPE).

我用叉子主()()将它们发送到工人功能每个进程分配相同的工作。

I use fork() in main() to assign each process the same work by sending them to the worker function.

void worker(int id) {
    .... (this piece of code is not relevant)

    if(write(pfd[id][1], &c, sizeof(c)) == -1)
        printf("Error occurred: %s\n",strerror(errno));

}

我怎样才能实现这个功能的信号捕捉SIGPIPE,并把它写入管道再次?

How can I implement signals in this function to catch SIGPIPE and make it write to the pipe again?

感谢您!

推荐答案

通常情况下,而不是追赶 SIGPIPE 人搭理它,这将导致失败, EPIPE ,而不是默默终止程序。

Typically, instead of catching SIGPIPE one ignores it, which causes write to fail with EPIPE instead of silently terminating your program.

然而如果您得到一个 SIGPIPE 当你写一个管道,那么就不要再试一次。它永远不会工作。 SIGPIPE 表示管道没有读者 - 如果管道现在已经没有读者,就永远不会有一个读者。 (想想这样?!怎么会没有读者的管道得到一个这是不可能的)

However: If you are getting a SIGPIPE when you write to a pipe, then do not try again. It will never work. SIGPIPE means that the pipe has no reader -- and if the pipe has no reader now, it will never have a reader. (Think about it this way: how would a pipe with no reader get one? It is impossible!)

您的问题是,你正在关闭管道的另一端。解决这个问题,而不用担心 SIGPIPE SIGPIPE 只是一个症状。

Your problem is that you are closing the other end of the pipe. Fix that, and don't worry about SIGPIPE. SIGPIPE is just the symptom.

编辑:这里要回答两个问题。如果你不能回答的两个的这些问题,那就不必处理 SIGPIPE

There are two questions to answer here. If you can't answer both of these questions, then don't bother handling SIGPIPE.


  1. 什么会导致我的程序来接收 SIGPIPE 以receieve唯一的办法 SIGPIPE 为管道的读出端得到关闭。会发生这种情况,如果读取处理崩溃,或者如果它被编程以关闭管道。如果你正在编写一个网络服务器,或与未知进程通信,这可能是常见的。但是,如果你写的这两个程序,无论是在本地运行,那么它可能表明一个编程错误。

  1. What would cause my program to receive SIGPIPE? The only way to receieve SIGPIPE is for the reading end of the pipe to get closed. This happens if the reading process crashes, or if it is programmed to close the pipe. If you are writing a network server, or communicating with an unknown process, this might be common. However, if you write both programs, both run locally, then it probably indicates a programming error.

当它捉住什么会我的程序做 SIGPIPE 如果你正在写一个使用管道进行通信客户端进程一台服务器,那么什么是你应该用 SIGPIPE 办?你不能再试一次,和客户通常无法重新启动它们连接到服务器。只是做明智的,默认的东西,让 SIGPIPE 终止程序。但是,如果服务器发送数据到客户端,它控制并得到 SIGPIPE ,它的可能的重新启动客户端。但是,这可能是一个非常糟糕的主意 - 例如,如果客户端是确定性的,它只会再次崩溃,你将最终获得一个无限循环,而不是一个简单的崩溃

What would my program do when it catches SIGPIPE? If you are writing a client process that uses a pipe to communicate with a server, then what are you supposed to do with SIGPIPE? You can't try again, and clients usually can't restart the server they're connected to. Just do the sensible, default thing and let SIGPIPE terminate your program. However, if the server is sending data to a client it controls and gets SIGPIPE, it could restart the client. But this might be a very bad idea -- for example, if the client is deterministic, it will just crash again, and you will end up with an infinite loop rather than a simple crash.

所以这里一般的格言是只有抓住你的ppared处理$ P $错误。不要捕获错误只是为了完整起见。只是让他们你的程序崩溃,或导致操作失败了,你可以回去和调试后来。

So the general maxim here is "Only catch errors you are prepared to handle." Don't catch errors just for the sake of completeness. Just let them crash your program, or cause the operation to fail, and you can go back and debug it later.

code片断:这是code从我的项目的一个片段。如果你运行它, SIGPIPE 不会终止进程。相反,将生成一个 EPIPE 错误。如果你正在编写一个网络服务器,那么 EPIPE 是一种可能的方式是客户端可能会突然断开连接。

Code snippet: This is a snippet of code from one of my projects. If you run it, SIGPIPE will not terminate your process. Instead, write will generate an EPIPE error. If you are writing a network server, then EPIPE is one possible way that a client might suddenly disconnect.

void
ignore_sigpipe(void)
{
    struct sigaction act;
    int r;
    memset(&act, 0, sizeof(act));
    act.sa_handler = SIG_IGN;
    act.sa_flags = SA_RESTART;
    r = sigaction(SIGPIPE, &act, NULL);
    if (r)
        err(1, "sigaction");
}

这篇关于使用信号和SIGPIPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:52