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"
1 On 06/03/2012 12:15 AM, Michał Górny wrote:
2 > On Sat, 02 Jun 2012 18:04:41 -0700
3 > Zac Medico <zmedico@g.o> wrote:
4 >
5 >> #!/usr/bin/env bash
6 >> named_pipe=$(mktemp -d)/fifo
7 >>
8 >> (
9 >> # hold the pipe open in read mode, so
10 >> # the writer doesn't block
11 >> sleep 3
12 >> ) < "$named_pipe" &
13 >
14 > I don't understand this part. This keeps the pipe open for reading
15 > which obviously causes it to lose data. If you open it, you need to
16 > read all that is there and then close.
17
18 The point is, there's always a small window of time between when a
19 reader reads its last byte, and when it finally closes the file
20 descriptor. During this window, there's a race where a writer can come
21 along and write something without blocking, and have that write be
22 destroyed when the previous reader closes the fd.
23
24 > And writers are supposed to be blocked. They are forked and just write
25 > when done, so there's no problem with keeping them alive for a short
26 > while.
27
28 Yeah, but you need locking if you want to prevent the race that I've
29 described above.
30 --
31 Thanks,
32 Zac