I’m trying to find a way to write an application with lock-free IPC on Linux, in C, with multi-core processors.
Let’s assume I have process 1 and process 2 that write to a FIFO or shared memory. Then process 3 and process 4 will read from that shared memory or FIFO.
Is this possible with a lock-free algorithm?
Your guidance is highly appreciated.
3
I have seen reference to using ring buffers, and controlled access the location pointers to eliminate or reduce the need for locks. It does not eliminate the need for waits though, and should only work then a ring buffer has one writer and one reader. In your case you would need at least two buffers.
The mechanism as I understand it is:
- writer waits until there is an open slot (last + 1 != current)
- writer writes entry in ring buffer.
-
writer updates last entry pointer.
-
reader waits until there is an available entry in the ring buffer (last != current).
- reader processes entry.
- reader increments current pointer.
Depending on the wait time used this can either add latency for new items in an empty queue, or burn a lot of CPU cycles in the wait loop. The order of updating the pointer and strict separation of write access is critical for this to work. The writer is only allowed to write the ring buffer entry before incrementing the pointer to make it available to the reader.
1
Yes it’s possible. We use one of the algorithms of the lock-free queue in our project. But these algorithms are really not obvious. If you want to understand them in depth you should be ready to spent couple months for it.
Inter process communication cannot be lock-free. Only inter-thread. As far as I am aware.
1