#include <iostream>
#include <cstring>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main(void) {
    int ret, p[2]; // p[0] lecture, p[1] écriture
    pipe(p);
    ret = fork();
    if(ret > 0) { // processus père, écrivain
        close(p[0]);
        for(char c = 'a'; c <= 'z'; c++) {
            cout << "(père) j'écris " << c << endl;
            write(p[1], &c, 1);
            usleep(rand()%20);
        }
        close(p[1]);
        waitpid(ret, NULL, 0);
    }
    else { // processus fils, lecteur
        char c;
        int i = 0;
        close(p[1]);
        while(read(p[0], &c, 1) == 1)
            cout << "(fils) je lis " << c << endl;
        close(p[0]);
    }
    return 0;
}

