On 06/03/2012 12:15 AM, Michał Górny wrote:
> On Sat, 02 Jun 2012 18:04:41 -0700
> Zac Medico <zmed...@gentoo.org> wrote:
> 
>> #!/usr/bin/env bash
>> named_pipe=$(mktemp -d)/fifo
>>
>> (
>>      # hold the pipe open in read mode, so
>>      # the writer doesn't block
>>      sleep 3
>> ) < "$named_pipe" &
> 
> I don't understand this part. This keeps the pipe open for reading
> which obviously causes it to lose data. If you open it, you need to
> read all that is there and then close.

The point is, there's always a small window of time between when a
reader reads its last byte, and when it finally closes the file
descriptor. During this window, there's a race where a writer can come
along and write something without blocking, and have that write be
destroyed when the previous reader closes the fd.

> And writers are supposed to be blocked. They are forked and just write
> when done, so there's no problem with keeping them alive for a short
> while.

Yeah, but you need locking if you want to prevent the race that I've
described above.
-- 
Thanks,
Zac

Reply via email to