1 |
张|武 wrote: |
2 |
> Hello. This might be OT but I am pretty interested in this and being |
3 |
> unlucky not able to find a real in-depth explanation of pipe on the |
4 |
> Internet. |
5 |
> |
6 |
|
7 |
I think "man 7 pipe" gives quite precise explanation for this. |
8 |
|
9 |
> How does pipe actually work? I mean, when there is a pipe like this: |
10 |
> $ appA | appB |
11 |
> What happen if appA produced output when appB is still busy processing |
12 |
> the data and did not require any data from input? |
13 |
> |
14 |
> possibility 1) as long as appA can get the resource (CPU?), it simply |
15 |
> keep outputing data, and this data is cached in memory, as long as there |
16 |
> is enough memory, and will finally feed to appB when appB finished his |
17 |
> business and begin to accept more data; |
18 |
> |
19 |
> possibility 2) as long as appB stop requiring data, appA is suspended, |
20 |
> the resource goes to appB. appA is only given resource (CPU?) when appB |
21 |
> finished his business and begin to accept more data; |
22 |
> |
23 |
|
24 |
I would say "both is true", except the limitation of (1) is not whole |
25 |
memory, but pipe buffer size (according to man it's 64k). When this buffer |
26 |
is full, (2) happens. |
27 |
|
28 |
> In my case appA gets the data from another host who have very short |
29 |
> timeout settings, appB is used to compress the data obtained from appA. |
30 |
> the compression is very difficult, usually at 30Kbps, the network is |
31 |
> very fast, around 10Mbps. appB compress the data tunck by tunck, if |
32 |
> Linux actually works in mode 2), the network connection is dropped when |
33 |
> the interval of two tuncks of appB compressing data is longer then the |
34 |
> network timeout setting. appA acutally don't know how to restart |
35 |
> connection from where it was dropped, thus understanding this difference |
36 |
> makes sense to me. |
37 |
|
38 |
If it's a problem, either appA or appB should be threaded, one thread |
39 |
buffering the the input in own memory (not relying on pipe buffer size) and |
40 |
second thread making the output (appA) or compression (appB). |
41 |
|
42 |
Caster |