Gentoo Archives: gentoo-dev

From: Zac Medico <zmedico@g.o>
To: "Michał Górny" <mgorny@g.o>
Cc: gentoo-dev@l.g.o, Brian Harring <ferringb@×××××.com>
Subject: Re: [gentoo-dev] multiprocessing.eclass: doing parallel work in bash
Date: Sun, 03 Jun 2012 07:19:29
Message-Id: 4FCB0FD2.9030607@gentoo.org
In Reply to: Re: [gentoo-dev] multiprocessing.eclass: doing parallel work in bash by "Michał Górny"
On 06/03/2012 12:15 AM, Michał Górny wrote:
> On Sat, 02 Jun 2012 18:04:41 -0700 > Zac Medico <zmedico@g.o> 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