pipe.c

#include <stdio.h>

#include <unistd.h>
#include <errno.h>

#include "utils.h"

int main(int argc, char *argv[])
{
    char buffer[100];
    int n;
    int fds[2];
    pipe(fds);
    switch(fork()) {
        case -1: exit_if(1, "Can't fork");
        case 0:
                 close(fds[0]);
                 n = snprintf(buffer, sizeof(buffer),
                         "I'm the child %d, of %d\n", getpid(), getppid());
                 write(fds[1], buffer, n);
                 printf("child is dead\n");
                 break;
        default:
                 close(fds[1]);
                 while((n = read(fds[0], buffer, sizeof(buffer) - 1)) > 0) {
                     buffer[n] = 0;
                     printf("father: %s", buffer);
                 }
                 break;
    }
    return 0;
}