Sample Header Ad - 728x90

Duplicate stdin to stdout and stderr, but in a synchronized way

3 votes
2 answers
2147 views
I need to duplicate the stdout of a producer and feed it to two consumer in a **synchronized** fashion. consumer 1 producer | duplicator | consumer 2 This can easily be accomplished for example via tee:
((cat f.txt | tee /dev/stderr | ./cons1.py >&3) 2>&1 | ./cons2.py) 3>&1
or via named pipes:
mkfifo fifo1 fifo2
cat f.txt | tee fifo1 fifo2 >/dev/null &

int main()
{
    char *line = NULL;
    size_t size;
    while (getline(&line, &size, stdin) != -1) {
        fprintf(stdout, "%s", line);
        fprintf(stderr, "%s", line);
    }
    return 0;
}
and then:
((cat f.txt | ./dup | ./cons1.py >&3) 2>&1 | ./cons2.py) 3>&1
However, **if consumer 1 is faster than consumer 2 we have a problem**. E.g., consumer 1 is already at line 50,000 while consumer 2 is at line 17,000. For my system **I need that both consumers are at the same line, hence the faster consumer needs to be restricted**. I know that this might be impossible via Linux standard tools. However, at least if we use that dup.c approach, it should be somehow possible. Any suggestions how to accomplish this? Thanks!
Asked by m33x (33 rep)
Sep 6, 2015, 12:06 PM
Last activity: Dec 14, 2023, 06:31 PM