Question: Please, have a look at this sample code which is using a very well established programming pattern to redirect `stdout` to a pipe.

```
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    int fd[2];
    pipe(fd);

    pid_t pid = fork();
    if (pid == 0) {
        close(fd[0]);
        dup2(fd[1], 1);
        printf("A string");
        _exit(0);
    }

    close(fd[1]);
    char text[1000];
    size_t size;
    int p = 0;
    while ((size = read(fd[0], text+p, 1)) == 1) {
        p++;
    }
    close(fd[0]);
    text[p] = '\0';
    printf("%s", text);

    return 0;
}

```

The code, as it is, is not working. As correctly suggested in a comment by @kaylum, calling `exit` instead of `_exit` in the child process makes the code working properly.

Answer: 
`exit()` flushes all the open streams as part its termination whereas `_exit()` doesn't flush - so any buffered output is lost when calling `_exit()`.

You can make it "work" either:

1) by calling `fflush(stdout);` to flush the buffered output *before* the call to `_exit()` or  

2) disabling the buffering for stdout with: `setbuf(stdout, 0);` at the beginning of the main().

POSIX requires that streams are flushed by [`exit()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html):

> 
> The exit() function shall then flush all open streams with unwritten
>  buffered data and close all open streams. Finally, the process shall
>  be terminated [CX] with the same consequences as described in
>  Consequences of Process Termination.
> 
> 
> 

Similarly, requires that streams are *not* flushed by [`_exit()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/_Exit.html):

> 
> The \_Exit() [CX] and \_exit() functions shall not call functions
>  registered with atexit() nor any registered signal handlers. [CX]
>  Open streams shall not be flushed. Whether open streams are closed
>  (without flushing) is implementation-defined. Finally, the calling
>  process shall be terminated with the consequences described below.
> 
> 
>
`_exit` terminates the process without performing the usual cleanup steps that `exit` (or returning from `main`) does. It's suggested for use in child processes for certain circumstances because it avoids performing incorrect or unnecessary cleanup; when forking a worker to perform a short task, the parent task might have unflushed I/O buffers, and using `_exit` prevents the same buffered data from being written by both the parent and the child, mysteriously duplicating output. It also prevents `atexit` handlers and the like from executing.

In a C++ program, [it also bypasses stack unwinding, so RAII cleanup (destructor invocation) doesn't occur either](http://www.cplusplus.com/reference/cstdlib/_Exit/); mostly meaningless for purely local resources (the child exits a little quicker, but that usually doesn't matter), but important if the cleanup affects externally visible state.