[back]
Linux Named Pipes
modified: 2015-05-10 22:33:52

1. Opening
2. Writing
3. Reading
4. Message Passing

The greatest strength of named pipes on Linux are there very simplistic setup code and usage. This makes them very easy to implement and the performance is very good. They however do some with some problems and restrictions.

Firstly, a named pipe must be created with mkfifo. A named pipe can have multiple readers and writers. It is a byte stream and is not message oriented.

1. Opening
    O_WRONLY                - will block if not opened for reading
    O_RDONLY                - will not block
    O_RDWR                  - will not block
    O_NONBLOCK | O_WRONLY   - will block if not opened for reading
    O_NONBLOCK | O_RDONLY   - will not block
2. Writing
    O_WRONLY                - will block until the data has been read
    O_WRONLY | O_NONBLOCK   - will not block but will buffer up to a certain amount
3. Reading
    O_RDONLY                - will block until data has been read
    O_RDONLY | O_NONBLOCK   - will not block and will read from buffer
4. Message Passing

Since a named pipe is treated as a byte stream the simplest method for passing messages is to use fixed sized messages. The second more flexible method is to use a size prefix to the message.

The issue of the atomicity of a write or read is at stake when we consider multiple readers and/or writers. For a fixed size message your message size simply needs to be under the size guarenteed to be atomic. For the flexible method the same restriction applies, but also that the message length and message are written and read at the same time.

A third method can be employed by using a master pipe to provide clients with an next avaible pipe or pair that is dedicated for that client, thus you only have one writer and one reader per pipe. Using this setup you can eliminate the concurrency problems and not have a message size limit.