0

Say we have a named pipe:

mkfifo my_named_pipe

say there are multiple writers writing to this named pipe:

node x.js > ${my_named_pipe} &
node y.js > ${my_named_pipe} &
node z.js > ${my_named_pipe} &

something like that - is there a reliable way to multiplex it, so that one whole message gets through each time, or can a named pipe reliable read from only one writer?

It leads me to wonder how we multiplex ports/sockets etc, I don't know how it's done.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

1

This might be a bit of a naïve answer, but this works for me.

If you have multiple writers to a single FIFO and you don't want their output to be mangled then you can use stdbuf but only if the output is line-based. Whole paragraphs will still be interleaved.

stdbuf -oL node x.js > ${my_named_pipe} &
stdbuf -oL node y.js > ${my_named_pipe} &
stdbuf -oL node z.js > ${my_named_pipe} &

man stdbuf: stdbuf - Run COMMAND, with modified buffering operations for its standard streams.

This will only work when your original program does not adjusts its buffering of the standard output stream.

kvantour
  • 25,269
  • 4
  • 47
  • 72