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

There are lot's of articles that introduces way of using pipe and redirecting standard IO with those.
But, whenever try to do this, there is always big issue - buffer mode!
See following example.

< Tested on [Ubuntu EGLIBC 2.12.1-0ubuntu9] + [Ubuntu 2.6.35-23-generic-pae] >

#include <stdio.h>
#define _tstr "Sample Text\n"
int
main () {
    int fdp[2];
    pipe (fdp);
    if (0 != fork ()) {/* parent */
        dup2 (fdp[1],1); /* redirect standard out */
        /* fdp is not used anymore */
        close (fdp[0]);
        close (fdp[1]);
        [*A] /* <--- see below */
        sleep(99999999);
    } else {
        int  rb;
        char buf[100];
        dup2 (fdp[0],0); /* redirect standard in */
        /* fdp is not used anymore */
        close (fdp[0]);
        close (fdp[1]);
        if (0 >= (rb = [*B])) perror("IO Error\n"); /* <-- see below for [*B] */
        buf[rb] = 0; /* add trailing 0 */
        printf ("read from input:%s\n", buf);
        sleep(99999999);
    }
}

< *** [*A][*B] pair and result. *** >
OK pairs
    [*A] : write (fdp[1], ...)   |   [*B] : read (fdp[0], ...)
    [*A] : write (1, ...)        |   [*B] : read (fdp[0], ...)
    [*A] : write (1, ...)        |   [*B] : read (0, ...)
    [*A] : printf (_tstr); fflush (stdout) | [B] : read (1, ...)

NOT OK pairs - printed output is "read from input:" ('_tstr' is not printed immediately)
    [*A] : printf (_tstr);       | [*B] : read (0, ...)
        -> 'fflush' is missing here. But '\n' is at the end of test string...

Why 'printf' doesn't work without 'flush'?
'printf' uses standard buffer (at first IO operation, buffer is allocated by using 'malloc').
And because, output device is pipe - not console, buffered mode is used.
So, until flushing, all outputs are stored in buffer (NOT device).
To make pipe be REALLY like standard IO, mode of those buffer should be LINE BUFFERED mode.
So, 'setvbuf() or setlinebuf()' should be used at the first of [*A] as follows.

[*A] : setlinebuf (stdout); printf (_tstr);
    OR setvbuf (stdout, (char*)NULL, _IOLBF, 0); printf (_tstr);

It is simple, isn't it?

Here is more complicated cases.
See following example.

< Tested on [Ubuntu EGLIBC 2.12.1-0ubuntu9] + [Ubuntu 2.6.35-23-generic-pae] >

< main.c >
#include <stdio.h>
int
main() {
    int fdp[2]; /* pipe */
    pipe (fdp);
    if (0 != fork()) { /* parent */
        dup2 (fdp[1], 1); /* redirect standard out */
        close (fdp[0]);
        close (fdp[1]); 
        [*C] /* <-- see below */
        execlp ("test", (char*)0); /* run test (*1) */
    } else {
        int  rb;
        char buf[100];
        dup2 (fdp[0],0); /* redirect standard in */
        /* fdp is not used anymore */
        close (fdp[0]);
        close (fdp[1]);
        if (0 >= (rb = [*B])) perror("IO Error\n");
        buf[rb] = 0; /* add trailing 0 */
        printf ("read from input:%s\n", buf);
        sleep(99999999);
    }

< test.c > => test (executable)
int
main () {
    printf("This is Test!\n");
    sleep(99999999);
}

As above case, string from 'test' - "This is Test!" - is not printed to console immediately because it is buffered.
(Assume that, < test.c > SHOULD NOT be modified!)
Is there solution? Yes.
Before moving next step, see this post first.

Combination of LD_PRELOAD and  __attribute__ ((constructor)) is solution.
To do this, new file is added to make share library that will be preloaded.

< mystdbuf.c > => libmystdbuf.so
#include <stdio.h>
__attribute__ ((constructor)) static void
mystdbuf () {
    setvbuf (stdout, (char*)NULL, _IOLBF, 0);
}

And add following codes to section [*C]

putenv ("LD_PRELOAD=./libmystdbuf.so");

Resolved!

Another easy and popular solution is using 'stdbuf' command in gnu core-utils.
Replace (*1) with

execlp ("stdbuf", "stdbuf", "-oL", "./test", (char*)0);

As described in LINK above, mechanism of 'stdbuf' is exactly same with above manual solution!
Done!

+ Recent posts