Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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've asked a question here before about multiple writers on a FIFO, and I know now that the write is thread safe as long as I write less than the PIPE_BIF, here is the link for that limit.

What about read? what if have two(or more) readers in multiple threads for reading from the same fifo, do I need locks here? or all I need is to read less than the PIPE_BUF?

BTW, I'm talking about Linux FIFO, And I'm using C.

share|improve this question
up vote 0 down vote accepted

Updated answer

Per the manual, you are NOT guaranteed atomic reads. However, their intent is for reads with FIFOs to be atomic. From paragraphs 2 and 4 of Rationale | Input and output

The standard developers considered adding atomicity requirements to a pipe or FIFO, but
 recognized that due to the nature of pipes and FIFOs there could be no guarantee of 
atomicity of reads of {PIPE_BUF} or any other size that would be an aid to applications 
I/O is intended to be atomic to ordinary files and pipes and FIFOs.

Until you see evidence otherwise, I would still be willing to say that you are fine.

Original answer

Based upon your previous question, I'm assuming all of your reads will be less than PIPE_BUF as well.

I don't believe you'll need any additional serialization around the reads since your unit of consumption is less than the guaranteed atomic, thread-safe write amount. If you had sequences of objects that needed to be read, then you would need some additional mechanism for serialization.

share|improve this answer
Yes I'll be writing and reading less the PIPE_BUF – poly Jul 27 '12 at 19:51
Is there a link to the spec or something that details that guarantee and the scope of the guarantee? – Telastyn Jul 27 '12 at 19:53
@Telastyn - for the read? I wasn't able to find anything when I was looking into the matter. The absence of information in the docs led me to believe that the same rules applied on the read as well. If it didn't operate that way, then it would be a crazy, undefined behavior that would border upon useless OR there would be huge disclaimers about how to use the read side of things. This particular scenario is less than the pipe buffer size. For reads larger than the pipe buffer, some sort of synch / lock would be required, I would imagine. – GlenH7 Jul 27 '12 at 20:19
I agree with Telastyn, if you read less than the PIPE_BUF then it's ok – poly Jul 27 '12 at 21:35

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.