Thursday, February 22, 2018

Interprocess Communication

Posix IPC is the only portable standard, but it suffers from lack of generality. Ideally, it should be easy to establish communication, with arguments to a single function, between zero or more processes or threads, identified by uid gid pid tid key and/or path, with optional blocking under various conditions, resumable or not, with or without filesystem presence, with any atomicity. A fundamental concept that was glossed over by Posix is whether the communication is zero one or many to zero one or many. Regular files are any to any, but require sideband book keeping to prevent the same user from rereading already processed communications. Named pipes prevent rereading of communications, but cannot have multiple simultaneous readers. Each kind of Posix IPC has blocking peculiarities, as if the blocking behavior was specified without user friendliness. If I don’t mind filesystem clutter, I can get a regular file to have atomic writes, and block on read from eof. Appends to the regular file go through a corresponding named pipe, so they are non blocking and atomic up to 4K. Each processor of the regular file tries for a writelock of effectively infinite length at eof. To allow for race conditions, check the file size after acquiring the lock, and retry if the lock is not at eof. If the writelock at eof is acquired, block on read from the named pipe. Upon read from the named pipe, append to the regular file, and release the writelock. If the attempt at writelock failed, wait for readlock of one byte after the last byte read. After acquiring the readlock, immediately release the readlock, and read to eof.