Gentoo Archives: gentoo-user

From: "Boyd Stephen Smith Jr." <bss03@××××××××××.net>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] how does a pipe work? Which process wait for which one, or they don't actually wait each other?
Date: Wed, 14 Jun 2006 17:01:00
Message-Id: 200606141138.57986.bss03@volumehost.net
In Reply to: [gentoo-user] how does a pipe work? Which process wait for which one, or they don't actually wait each other? by "张韡武"
1 On Wednesday 14 June 2006 08:12, 张韡武 <zhangweiwu@××××××.com> wrote
2 about '[gentoo-user] how does a pipe work? Which process wait for which
3 one, or they don't actually wait each other?':
4 > How does pipe actually work? I mean, when there is a pipe like this:
5 > $ appA | appB
6 > What happen if appA produced output when appB is still busy processing
7 > the data and did not require any data from input?
8 >
9 > possibility 1) as long as appA can get the resource (CPU?), it simply
10 > keep outputing data, and this data is cached in memory, as long as there
11 > is enough memory, and will finally feed to appB when appB finished his
12 > business and begin to accept more data;
13 >
14 > possibility 2) as long as appB stop requiring data, appA is suspended,
15 > the resource goes to appB. appA is only given resource (CPU?) when appB
16 > finished his business and begin to accept more data;
17 >
18 > Which one is true? I know usually 1) and 2) makes no difference to most
19 > users, that's why the detail explanation of "1) or 2)" is so hard to
20 > google out.
21
22 Neither! Both!
23
24 The implementation of pipes varies from *NIX to *NIX, and possibly within
25 the same *NIX, since a shell might 'enhance' the pipes provided by the
26 kernel/libc. (The shell binary is ultimately responsible for implementing
27 the pipe, so it may arbitrarily 'decorate' a standard pipe.)
28
29 In any case, you can't depend on any particular behavior if you want your
30 shell script to be portable.
31
32 In linux/bash I believe it works like this: Each, pipe has a small (~1
33 page) FIFO buffer in memory. (Not sure if this is kernel or userspace.)
34 Both processes are started and compete for CPU time in the standard way.
35 Either process may block on I/O when it performs standard, blocking I/O on
36 the pipe. appA will block if the FIFO gets full; appB will block if the
37 FIFO gets empty.
38
39 If you really must know: Use the Source, Luke.
40
41 > In my case appA gets the data from another host who have very short
42 > timeout settings, appB is used to compress the data obtained from appA.
43 > the compression is very difficult, usually at 30Kbps, the network is
44 > very fast, around 10Mbps. appB compress the data tunck by tunck, if
45 > Linux actually works in mode 2), the network connection is dropped when
46 > the interval of two tuncks of appB compressing data is longer then the
47 > network timeout setting. appA acutally don't know how to restart
48 > connection from where it was dropped, thus understanding this difference
49 > makes sense to me.
50
51 This also depends a lot on the application. appA can use asynchronous I/O,
52 provide a larger buffer (perhaps even a temporary file), and/or send
53 keepalives through the network. Also, appB's compression my be
54 interrupted while more data is written to the buffer.
55
56 > I made several experiements and my appA and appB both works fine, but I
57 > don't dare to share this appA/B to others unless I get the mechnism
58 > understood.
59
60 With the speeds you mention, the timeout would have to be ~8s or less for
61 the socket to be dropped.[1] Once a socket is established, they are
62 amazingly stable; timeout for an established socket is usually more like
63 5-10 minutes or even an hour. Heck, I think OBSD 3.8 defaulted to a 1 DAY
64 timeout before the OS reaped an established socket.
65
66 Also, you generally want to compress stuff before putting it on the wire,
67 not after...
68
69 --
70 "If there's one thing we've established over the years,
71 it's that the vast majority of our users don't have the slightest
72 clue what's best for them in terms of package stability."
73 -- Gentoo Developer Ciaran McCreesh
74
75 [1] That's assuming 15Kbps compression rate and the ability to send
76 full-size 16KB ip packets. Most likely, ~1s would suffice, since packets
77 are generally 1500B ~= 15Kb in size.