Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to use FIFO in my application in linux+C, I have multiple threads and processes, I read some where that writing to a FIFO is atomic below some value (I believe 4k) especially I'm writing max of 300 bytes, Am I safe here?

share|improve this question
up vote 1 down vote accepted

It's atomic only inasmuch as how you write your FIFO queue/structure.

Wrapping a mutex or some other form of lock around the FIFO queue is probably the easiest way to guarantee concurrency and atomicity for the environment you describe.

If you'll have multiple processes attempting to write to the queue, then you'll probably need a dedicated handler for the queue.

Option A.

You could set up two write areas for the queue - the first area is where any and all processes can write to, synchronized with some sort of indexing scheme, or:

Option B.

It would probably be cleaner to have a dedicated process for the queue that has multiple threads to receive write requests. Synchronization on writing to the queue is then handled within the FIFO queue process itself.

As a rule, unless you have documentation in your hand that declares operation Foo to be thread-safe/atomic/whatever, then assume it isn't safe. Having "read somewhere ..." isn't going to cut it when you've got race conditions and the data in your queue has been shredded from simultaneous writes.

Option C.

Use the Pipe/FIFO structure provided by the OS.

Yes, you'll be fine/safe. From the first link on Pipes & FIFOs that you provided in the comments:

POSIX.1-2001 says that write(2)s of less than PIPE_BUF bytes must be atomic:

And from the limits.h you linked:

 13 #define PIPE_BUF        4096    /* # bytes in atomic write to a pipe */

Since you're writing <= 300 bytes, and the POSIX minimum buffer size for an atomic write is 512 bytes, you're very much in the clear. To be paranoid, you could double check the PIPE_BUF size in your local limits.h, but it's not really necessary.

Your writes are guaranteed thread-safe since you're writing less than the pipe buffer size. Likewise, your reads should be okay and/or they should have the benefit of device locking preventing two threads from accessing at the same time.

share|improve this answer
I believe the original poster is referring to the FIFO objects of Linux/POSIX, which are pipes referred to by a filesystem entry. – Lars Viklund Jul 4 '12 at 12:07
Yes you're right Lars, I mean posix/linux, thank Glen, I found this where it mentioned that it is atomic and also found it in the linux source code but can't find now, the reason I asked the question because it seems no one is sure what will happen but it's mentioned as an atomic operation if you wright less than the fifo buffer – poly Jul 4 '12 at 15:07
found it, according to this it should be threads/processes safe, what do you think? – poly Jul 4 '12 at 15:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.