Using signals and sigpipe

后端 未结 1 654
生来不讨喜
生来不讨喜 2021-01-01 03:18

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

I\'m working right now on th

相关标签:
1条回答
  • 2021-01-01 04:03

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

    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!)

    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.

    Edit: There are two questions to answer here. If you can't answer both of these questions, then don't bother handling 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.

    2. 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.

    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 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");
    }
    
    0 讨论(0)
提交回复
热议问题