Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there repercussions to having many processes write to a single reader on a named pipe in posix?

I am writing a program for POSIX (OSX) where I will have many processes sending messages to one listener, who is essentially a logging thread. All of the processes are running in seperate programs, and using a single named pipe (FIFO) that many processes write to, but only a single process reads from is very tempting.

Questions:

1) Will this work? - I can make this work using bash to setup a fifo with multiple processes writing to it, so I know in theory this works. But in practice, are there issues I'm glossing over?

shell #1

$ mkfifo /tmp/fifo
$ cat /tmp/fifo

shells #2 and #3

$ cat > /tmp/fifo
"Type stuff here, after hitting enter, it is read by shell #1"

2) If each writer only writes relatively short messages (< 100 bytes?) then can I assume that each call to write() will will be sent to reader in it's entirety? Or will half of one message be at risk of garbled with half of another message from a different writer?

thanks for any advice.

like image 331
Aftermathew Avatar asked Feb 25 '09 20:02

Aftermathew


People also ask

Can multiple processes write to the same named pipe?

Yes, multiple processes can read from (or write to) a pipe. But data isn't duplicated for the processes.

Can a pipe have multiple readers?

Unlike sockets, it's not possible to broadcast data to multiple readers using named pipes. Unlike anonymous pipes which exist as long as the process exists, a named pipe can exist as long as the file exists in the file system.

What are the advantages of named pipes over unnamed pipes?

A named pipe provides many-to-many, two-way communication between one or more processes that are not necessarily related and do not need to exist at the same time. The file name of the pipe serves as an address or contract between the processes for communication.

Can named pipes be bidirectional?

Named pipes may be used to pass data between unrelated processes, while normal (unnamed) pipes can only connect parent/child processes (unless you tryvery hard). Named pipes are strictly unidirectional, even on systems where anonymous pipes are bidirectional (full-duplex).


1 Answers

The FIFO write should be atomic, as long as it's under the page size. So there shouldn't be an issue with 100 bytes messages. On linux the max size used to be 4K, I believe it is larger now. I've used this technique on a few systems for message passing, since the writes end up atomic.

You can end up with an issue, if you are using a series of writes, since output buffering could cause a sync issue. So make sure the whole message is written at one time. eg. build a string, then print, don't print multiple pieces at once.

s="This is a message"
echo $s

NOT

echo "This "
echo "is "
echo " a message"
like image 177
sfossen Avatar answered Oct 19 '22 03:10

sfossen