Using signals and sigpipe
Asked Answered
S

1

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!

Squiggle answered 14/10, 2011 at 23:21 Comment(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.Mcgough
oh sorry! i'll do that. EDIT: done! i'm new to this site and how it works but i got it now :)Squiggle
M
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");
}
Mcgough answered 15/10, 2011 at 12:19 Comment(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"Squiggle
If you're not getting SIGPIPE, then why do you need to handle it?Mcgough
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 :)Squiggle
@Squiggle 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.Kiely
It is impossible! What about named pipes?Kiely
@user185953: The question is about pipes, not fifos.Mcgough
@DietrichEpp My mistake. I thought I can use linkat() to name unnamed pipes, but that would be insecure, because I would be able to linkat() my reading end of a pipe, chmod 666, open O_WRONLY and I would get the writing end.Kiely

© 2022 - 2024 — McMap. All rights reserved.