IPC in C under linux

cmultithreading

I'm building a messaging solution with the followingsetup: all the messages are saved on a DB, two or more reader processes will read from this DB and send data to other process(es) which will send it over the network.

My approach is depicted below,

The following have 4 sender process with 4 fifos, and 2 readers with 2 fifos

reader0 ← read data from DB
reader1 ← read data from DB

sending part
network_handler0 ← network_handler_fifo0 ← reader0
network_handler1 ← network_handler_fifo1 ← reader1
network_handler2 ← network_handler_fifo2 ← reader0
network_handler3 ← network_handler_fifo3 ← reader1

receiving part
network_handler0 → reader_fifo0 → reader0 → write to DB
network_handler1 → reader_fifo1 → reader1 → write to DB
network_handler2 → reader_fifo0 → reader0 → write to DB
network_handler3 → reader_fifo1 → reader1 → write to DB

I have few problem with this setup, and please note that the number of processes could be more than that based on the environment, so I could make it 20 readers and 10 network_handlers or it it could as shown above.

  1. The size of the buffer is 64K and the message size is 200k, is this small enough to make the write/read to/from fifo atomic?

  2. How can make the processes aware of each other, so for example, reader 0 writes to network_handler_fifo0 and network_handler_fifo2, how can I make it start writing on other fifo if the current ones are full or their network_handlers are dea
    d

  3. I thought about making the reader process writing more general in writing, so for example it writes to all network fifos using lock mechanism and stop writing on the one that its process dead, I didn't use it as lock mechanism could slow thing down.

BTW, each network_handler is an SCTP association, so network_handler0 is association 0, network_handler1 is association 1 and so on.

Any idea is appreciated. I mean even if I have to change the setup above.

Best Answer

Firstly, nothing is atomic in multi-processing situations. Do not expect to be able to ignore synchronisation just because you're using a relatively small payload. (even a 4-byte payload needs protection).

You might like to look at existing systems for this, I'd recommend ZeroMQ that offers you everything you need for this question.