Merge pull request #7602 from YPBlib/YPBlib_Finish_Translating

Finish_Translating_20180131 Why you should use named pipes on Linux
This commit is contained in:
Xingyu.Wang
2018-02-05 21:20:18 +08:00
committed by GitHub

View File

@@ -1,22 +1,19 @@
#Being translated by YPBlib
======
Why you should use named pipes on Linux
为什么应该在Linux上使用命名管道
======
![](https://images.techhive.com/images/article/2017/05/blue-1845806_1280-100722976-large.jpg)
Just about every Linux user is familiar with the process of piping data from one process to another using | signs. It provides an easy way to send output from one command to another and end up with only the data you want to see without having to write scripts to do all of the selecting and reformatting.
估计每一位Linux使用者都熟悉使用 “|”符号将数据从一个进程传输到另一个进程的操作。它使用户能简便地从一个命令输出数据到另一个命令并筛选出想要的数据而无须写脚本进行选择、重定格式等操作。
There is another type of pipe, however, one that warrants the name "pipe" but has a very different personality. It's one that you may have never tried or even thought about -- the named pipe.
还有另一种管道, 虽然也叫“管道”这个名字却有着非常不同的性质。即您可能尚未使用甚至尚未知晓的——命名管道。
**Also read:[11 pointless but awesome Linux terminal tricks][1]**
**另可参考:[11 pointless but awesome Linux terminal tricks][1]**
One of the key differences between regular pipes and named pipes is that named pipes have a presense in the file system. That is, they show up as files. But unlike most files, they never appear to have contents. Even if you write a lot of data to a named pipe, the file appears to be empty.
普通管道与命名管道的一个主要区别就是命名管道是它们以文件形式实实在在地存在于系统中存在。但是与其它文件不同的是,命名管道文件似乎从来没有文件内容。即使用户往命名管道中写入大量数据,该文件看起来还是空的。
### How to set up a named pipe on Linux
### 如何在Linux上创建命名管道
Before we look at one of these empty named pipes, let's step back and see how a named pipe is set up. You would use a command called **mkfifo**. Why the reference to "FIFO"? Because a named pipe is also known as a FIFO special file. The term "FIFO" refers to its first-in, first-out character. If you fill a dish with ice cream and then start eating it, you'd be doing a LIFO (last-in, first-out) maneuver. If you suck a milkshake through a straw, you'd be doing a FIFO one. So, here's an example of creating a named pipe.
Before在我们研究这些空命名管道之前,先追根溯源来看看命名管道是如何被创建的。您可以使用**mkfifo** 命令。提到“FIFO”是因为命名管道也被认为是FIFO特殊文件。术语“FIFO”指的是它的先进先出("first-in, first-out ")属性。如果某人在吃一个甜筒冰淇淋那么他执行的就是一个LIFO(后进先出last-in, first-out)操作。如果他通过吸管饮食一个冰淇淋那就在执行一个FIFO操作。好接下来是一个创建命名管道的例子。
```
$ mkfifo mypipe
$ ls -l mypipe
@@ -24,7 +21,7 @@ prw-r-----. 1 shs staff 0 Jan 31 13:59 mypipe
```
Notice the special file type designation of "p" and the file length of zero. You can write to a named pipe by redirecting output to it and the length will still be zero.
(译者注:实际环境中当您输入`mkfifo mypipe`后命令行可能会阻塞在当前位置这是因为以只写方式打开的FIFO要阻塞到其他某个进程以读方式打开该FIFO。您可在另一个终端中输入 `cat < mypipe`解决注意两个终端中保证mypipe的路径一致。注意一下特殊的文件类型标记“p”以及文件大小为0。您可以将重定向数据写入命名管道文件而文件大小依然为0。
```
$ echo "Can you read this?" > mypipe
$ ls -l mypipe
@@ -32,23 +29,23 @@ prw-r-----. 1 shs staff 0 Jan 31 13:59 mypipe
```
So far, so good, but hit return and nothing much happens.
正如上面所说,敲击回车似乎什么都没有发生。
```
$ echo "Can you read this?" > mypipe
```
While it might not be obvious, your text has entered into the pipe, but you're still peeking into the _input_ end of it. You or someone else may be sitting at the _output_ end and be ready to read the data that's being poured into the pipe, now waiting for it to be read.
不那么显然的是,用户输入的文本已经进入该命名管道。可能用户却还在盯着输入端,事实上应有用户或其他人在输出端读取接受数据。
```
$ cat mypipe
Can you read this?
```
Once read, the contents of the pipe are gone.
一旦被读取之后,管道中的内容就不再被保存。
Another way to see how a named pipe works is to perform both operations (pouring the data into the pipe and retrieving it at the other end) yourself by putting the pouring part into the background.
另一种研究命名管道如何工作的方式是通过在后台传送数据的方式完成传送和接受两样操作。
```
$ echo "Can you read this?" > mypipe &
[1] 79302
@@ -58,21 +55,21 @@ Can you read this?
```
Once the pipe has been read or "drained," it's empty, though it still will be visible as an empty file ready to be used again. Of course, this brings us to the "why bother?" stage.
一旦管道被读取或“耗干”,该管道就清空了,尽管我们还能看见它并再次使用,可为什么要费此周折呢?
### Why use named pipes?
### 为何要使用命名管道?
Named pipes are used infrequently for a good reason. On Unix systems, there are almost always many ways to do pretty much the same thing. There are many ways to write to a file, read from a file, and empty a file, though named pipes have a certain efficiency going for them.
命名管道很少被使用的理由似乎很充分。毕竟在Unix系统上总有多种不同的方式完成同样的操作。有多种方式写文件、读文件、清空文件尽管命名管道比他们来得更高效。
For one thing, named pipe content resides in memory rather than being written to disk. It is passed only when both ends of the pipe have been opened. And you can write to a pipe multiple times before it is opened at the other end and read. By using named pipes, you can establish a process in which one process writes to a pipe and another reads from a pipe without much concern about trying to time or carefully orchestrate their interaction.
值得注意的是,命名管道的内容驻留在内存中而不是被写到硬盘上。数据内容只有在输入输出端都打开时才会传送。用户可以在管道的输出端打开之前向管道多次写入。通过使用命名管道,用户可以创建一个同时有一个进程写入管道和一个进程读取管道的进程而不用关心协调二者时间上的同步。
You can set up a process that simply waits for data to appear at the output end of the pipe and then works with it when it does. In the command below, we use the tail command to wait for data to appear.
用户可以创建一个单纯等待数据出现在输出端的管道,并在拿到输出数据后对其进行操作。
```
$ tail -f mypipe
```
Once the process that will be feeding the pipe has finished, we will see some output.
一旦喂给管道数据的进程结束了,我们就可以看到一些输出。
```
$ tail -f mypipe
Uranus replicated to WCDC7
@@ -82,7 +79,7 @@ Server replication operation completed
```
If you look at the process writing to a named pipe, you might be surprised by how little resources it uses. In the ps output below, the only significant resource use is virtual memory (the VSZ column).
如果研究一下向命名管道写入的进程,用户也许会惊讶于它的资源消耗之少。在下面的`ps`命令输出中唯一显著的资源消耗是虚拟内存VSZ那一列
```
ps u -P 80038
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
@@ -90,14 +87,14 @@ shs 80038 0.0 0.0 108488 764 pts/4 S 15:25 0:00 -bash
```
Named pipes are different enough from the more commonly used Unix/Linux pipes to warrant a different name, but "pipe" really invokes a good image of how they move data between processes, so "named pipe" fits pretty well. Maybe you'll come across a task that will benefit significantly from this very clever Unix/Linux feature.
命名管道与Unix系统上更常用的管道相比足以不同到拥有另一个名号但是“管道”实在能反映出它们如何在进程间传送数据的形象故将称其为“命名管道”还真是恰如其分。也许您在执行操作时就能从这个聪明的Unix特性中获益匪浅呢。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3251853/linux/why-use-named-pipes-on-linux.html
作者:[Sandra Henry-Stocker][a]
译者:[译者ID](https://github.com/译者ID)
译者:[译者ID](https://github.com/YPBlib)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出