ShmFIFO library provides an easy-to-use interface to shared memory for programs where one process needs to send blocks of data to other processes.
It was developed because pipe(2) and mkfifo(3) have a very small buffer size (4k) and are unsuitable for many applications. Shmfifo allows you to put a block of data in shared memory, get the oldest block of data from shared memory.
ShmFIFO library also has the feature to share one instance of a private data structure among all processes which use the library.
How to use shmfifo
Before using, shared memory should be created. After creating shared memory, process which wants to use it, should attach to memory. Then it can put and get blocks to FIFO. After process is finished working with FIFO, it calls shfifo_detach. When no processes will use FIFO, shared memory should be deallocated.
Usally, lifecycle of shmfifo-based program is following:
[both] shmfifo_put, shmfifo_get (many times)
[parent] wait(2) or waitpid(2)
shmfifo comes with test.c program, which is good sample of how to write programs with shmfifo. It forks into 2 processes, parent generated variable-length blocks, writes checksum into each block and put it into FIFO. Child gets blocks from fifo, check if checksum is valid (it's always valid if there is no bug in program) and prints debug info. after large number of blocks gets transferred, both processes are exit.
Additionally, shmfifo allows processes to share one private structure. test.c uses this structure to store counter, which is increased each time when parent cannot put block to FIFO because it's already full and has to wait until child will get block. If small amount of memory allocated for FIFO, then parent will wait more often.
test.c is sample test program which is builts into 'test' binary. You can run it to test if library is working