preserving message boundaries on named pipes - System V

John Cowan cowan at marob.MASA.COM
Sat May 20 05:42:33 AEST 1989


In article <683 at maxim.erbe.se> prc at maxim.UUCP (Robert Claeson) writes:
>In article <571 at lehi3b15.csee.Lehigh.EDU> murrey at lehi3b15.csee.Lehigh.EDU (Erik Murrey) writes:
>
>>Is it possible to preserve message boundaries on named pipes under
>>System V?  For instance, if a process sends two separate messages (via
>>two write()'s) down a named pipe, how can the receiver read those as
>>two separate messages?
>
>You can't, as far as I know. I'd suggest you to take a look at message
>queues instead. They are also more efficient than pipes since they don't
>deal with the file system at all.


Claeson is technically correct.  However, it is an undocumented, but reliable,
property of the 'write' system call on a pipe that it is atomic if the
number of bytes written is less than a certain magic number, typically
10240.  Therefore, a message protocol can be superimposed on the regular
named pipe bytestream by some such scheme as "prefix each message by its
length" or "delimit each message with a reserved character".  No mixing of
messages will occur, as long as the above length restriction is observed.

These remarks apply equally to named and regular pipes, and to all versions
of Unix, modulo the exact value of the magic number.
-- 
John Cowan <cowan at marob.masa.com> or <cowan at magpie.masa.com>
UUCP mailers:  ...!uunet!hombre!{marob,magpie}!cowan
Fidonet (last resort): 1:107/711
Aiya elenion ancalima!



More information about the Comp.unix.wizards mailing list