5

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

}

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

Thank you!

2
  • You should go back and accept answers for questions that you've asked. People are unlikely to answer your questions since you don't accept answers for them. Oct 15, 2011 at 12:20
  • oh sorry! i'll do that. EDIT: done! i'm new to this site and how it works but i got it now :)
    – andrepcg
    Oct 15, 2011 at 12:58

1 Answer 1

11

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");
}
7
  • My program is working fine and i'm not getting any errors (not even SIGPIPE). I was thinking about "if i ever get SIGPIPE"
    – andrepcg
    Oct 15, 2011 at 12:57
  • 1
    If you're not getting SIGPIPE, then why do you need to handle it? Oct 15, 2011 at 12:59
  • I don't know, but if the program would be distributed for a lot of computers and one of those gets SIGPIPE I would be a good programmer by preventing these cases. That's what I think. If I'm not right then tell me :)
    – andrepcg
    Oct 15, 2011 at 13:26
  • @andrepcg Terminaning on sigpipe is usually good behavior. For example in cat bigfile | grep -v ballast | head -n 100 it is correct that grep and cat terminate when they receive SIGPIPE. Otherwise they would either hang or process rest of the big file unnecessarily.
    – user185953
    Mar 9, 2021 at 9:51
  • It is impossible! What about named pipes?
    – user185953
    Mar 9, 2021 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.