In Linux, read or write side of pipe is automatically closed if there is no reference for the other side - file is really closed.
But I faced with strange case when implementing a 'ylisp' function - pipe is automatically closed even if there is still valid file descriptor that references that pipe.
Here is simpler version of issued code.

/* function */
{
    int   fd[2];
    pid_t cpid;
    pipe (fd);
    cpid = fork();
    if (cpid == 0) {
        close (fd[0]); /* close read end */
        dup2 (fd[1], STDIN_FILENO);
        /* --- (*a) --- */
        close (fd[0]); /* <-- (*1) */
        /* --- (*b) --- */
        ...
        execvp (...)
    } else {            /* Parent writes argv[1] to pipe */
        close (fd[1]); /* close write end */
        ... /* read end is used */
    }
    ...
}

ylisp is multi-threaded program. And building/running environment is

OS : Linux 2.6.35-24-generic-pae #42-Ubuntu SMP Thu Dec 2 03:21:31 UTC 2010 i686 GNU/Linux
Compiler : gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
libc : Ubuntu EGLIBC 2.12.1-0ubuntu10.1

For testing, I put test code to check whether STDIN_FILENO is valid file descriptor or not.
Interestingly, sometimes, STDIN_FILENO is invalid at (*b), even if it is valid at (*a).
I don't have any idea what happens to this code.
Fortunately, commenting out (*1) seems to be a walk-around for this issue, but it's just temporal solution.
'fd[0]' is never closed in this walk-around.

I need to look into this with more time... very interesting...

+ Recent posts