I need to make a named pipe with mkfifo. For instance:

mkfifo mypipe

I'm going to have a systemd service (a simple shell script) that read from the pipe and I will write on the same pipe calling another script (e.g. write-to-pipe.sh).

What if I have multiple instances of write-to-pipe.sh writing simultaneously on the pipe? I mean, many users can execute write-to-pipe.sh. If two users try to write on the pipe at the same time, is it going to create trouble (like when two users try to write on a regular file (not pipe) and one overwrite the other) or not?

I hope this is not a duplicate, but I couldn't find an answer to this question...


  • There will be trouble if the whatever's on the other end of pipe expects complete discrete messages.
    – muru
    Jun 30 '18 at 7:08
  • @muru, every message written by the shell script will be only one line. At the other end of the pipe I will expect to read every line as a message. Will a single line be written correctly or mixed with other lines from other instances of the script?
    – mugnozzo
    Jun 30 '18 at 7:13
  • 1
    @mugnozzo I have the same problem and with systemd! Did you come up with a solution ? if so, can you please post what worked for you?
    – vyom
    Aug 4 '20 at 9:32
  • @vyom Unfortunately not. Still don't have a solution.
    – mugnozzo
    Aug 13 '20 at 14:03
  • 1
    @mugnozzo I switched to POSIX MQ, that worked for me.
    – vyom
    Aug 13 '20 at 15:42

Here's a quick test I ran:

mkfifo foo
yes "Process 1 reporting" > foo & yes "This is process 2" > foo &
awk '!a[$0]++' < foo

And the output I got from awk:

Process 1 reporting
Process 1 reportThis is process 2
This is process 2
This is pring
Process 1 reportocess 2
ocess 2
This is prProcess 1 reporting
  • There is a problem here. I think it is related to awk. Since writing into FIFO up to size of PIPE_BUF defined in limits.h must be atomic.
    – snr
    Apr 17 '19 at 14:31
  • @snr and what happens when PIPE_BUF % len(sentence) != 0?
    – muru
    Apr 17 '19 at 14:42
  • could you elucidate what you try to take to mean by saying PIPE_BUF % len(sentence) != 0 ??
    – snr
    Apr 17 '19 at 14:52
  • I mean a whole number of sentences don't fit in a buffer of size PIPE_BUF
    – muru
    Apr 17 '19 at 15:06
  • At that case, the one you show as output is possible. But in your example you don't fill the size. Do you?
    – snr
    Apr 17 '19 at 15:09

Use something like sem from GNU Parallel:

mkfifo myfifo

sem --id myid 'seq 1 10; sleep 1; echo done' > myfifo &
sem --id myid 'seq 2 10; sleep 1; echo done' > myfifo &
sem --id myid 'seq 3 10; sleep 1; echo done' > myfifo &
sem --id myid 'seq 4 10; sleep 1; echo done' > myfifo &

cat myfifo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.