Check if the pipe has broken before trying to write it?

Is it possible to check if the channel was damaged before trying to write / read it, so I can just skip it and continue with the program?

I use a loop whileto write to channels that transmit from a parent to several children. During the cycle, some of the children will be closed. When the cycle comes and tries to write to them again, my program shuts down because it is killed by SIGPIPE, because the pipe is broken. I know that the pipe is broken, I programmed the children to close their pipes and exit (necessary). I still want to end the loop and go to the program. I would like it to check if the channel is damaged, skip it if it is broken (without error output), and go to the program (other children should still be recorded).

So is this possible? The example in c will be great.

Here's a simple pseudo-code representation of what I am asking:

int i = 0;

while(i != 5)
{
    if (mypipe[WRITE] is BROKEN)
        //Do Nothing ---Basically Skip

    else if (some condition)
        write(mypipe[WRITE]);

    i++;
}

, . .

+4
3

, SIGPIPE

, , SIGPIPE, write() , :

  signal(SIGPIPE, SIG_IGN);

ssize_t rc;
rc = write(...);
if (rc == -1) {
    if (errno == EPIPE) {
         //it broken
    }
 //some other error occured.
}
+5

, SIGPIPE, :

/* initialization */
signal(SIGPIPE, SIG_IGN);
...

/* try writing to pipe and detect broken pipe */
ssize_t written = write(mypipe[WRITE], buf, size);
if (written == -1 && errno == EPIPE)
  ... remove pipe from the list and continue processing
+4

, , ​​ poll (2). , ( - read) ( write), .

( select (2) syscall, poll - C10K)

, - - .

. . .

+3

All Articles