Are Unix / Linux vendors manufacturers or consumers?

Suppose I have this:

A | B | C

How does the pipeline work? Does A produce data only when B requests it? Does A continuously generate data and then block if B cannot currently accept it? What is C? I realized that the system that I am developing is conceptually very similar to these pipelines - I would like to use the existing paradigm, and not invent something new that only works halfway.

+7
linux unix bash shell pipe
source share
2 answers

Pipes on Unix have a buffer, therefore, even if the right process (RSP) does not consume any data, the left process (LSP) is able to issue several kilobytes before locking.

Then, if the buffer is full, the LSP is blocked. When the RSP reads the data, it frees up part or all of the buffer space, and the LSP resumes work.

If instead of 2 processes you have 3, the situation is more or less the same: a faster producer is blocked by a slower consumer. And, obviously, a faster consumer is blocked by a slower producer if the channel becomes empty: just think about the interactive shell, waiting for the slowest producer of everything: the user.

For example, the following command:

 $ yes | cat | more 

Since more blocked when the screen is full until the user presses a key, the cat process will fill its output buffer and stop, then the yes process will fill its buffer and also stop. Everything that awaits the user, as it should be.

PS: I wonder what happens when the more process ends? well, the right side of this pipe is closed, so the cat process will receive a SIGPIPE signal (if it is ever recorded again in the pipe, and it will) and die. The same thing will happen with the yes process. All processes die, as it should be.

+12
source share

A has a pipe to B, and B has a pipe C. Each pipe has a buffer; B and C if they are trying to read and there is no input available (end-of-stream counter as input). A and B, if they have an output for recording, but the channel buffer is full.

All three processes are performed simultaneously, using as much CPU as possible. The OS blocks them during a read / write system call as necessary if the stream buffer is exhausted / full accordingly.

Thus, they are controlled by both the consumer and the manufacturer, that is, the rate is the minimum consumption rate and production rate. If the consumer is faster, the performance is determined by the manufacturer and vv.

+2
source share

All Articles