Linux Multithreading – Shared FIFO File Descriptor

clinuxmultithreading

is ok to open fifo with one FD and share it with multiple threads?
or is it better to have multiple fds opened for the same fifo and share these fds with the threads?
BTW, I'll be doing write and read.

The environment is linux, C, pthreads

Best Answer

Depends on what exactly you are going to do. Usually if you stick to the syscall-level interface (int-typed descriptors, methods read and write), one descriptor is ok, except if you want to have different settings (some blocking, some not and such).

Remember, that when there are multiple readers on fifo, random one of them will get the data and the switch from one reader to another may happen after any byte (because it depends on write granularity and internal implementation of the buffer in kernel), so you either need one fifo for each intended recipient or use single-byte messages.