在 Linux Bash 中使用命名管道的示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4113986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Example of using named pipes in Linux Bash
提问by Drew LeSueur
Can someone post a simple example of using named pipes in Bash in Linux?
有人可以发布一个在 Linux 中的 Bash 中使用命名管道的简单示例吗?
采纳答案by Brian Clements
One of the best examples of a practical use of a named pipe...
命名管道实际使用的最佳示例之一...
From http://en.wikipedia.org/wiki/Netcat:
来自http://en.wikipedia.org/wiki/Netcat:
Another useful behavior is using
netcat
as a proxy. Both ports and hosts can be redirected. Look at this example:nc -l 12345 | nc www.google.com 80
Port 12345 represents the request.
This starts a
nc
server on port 12345 and all the connections get redirected togoogle.com:80
. If a web browser makes a request tonc
, the request will be sent to google but the response will not be sent to the web browser. That is because pipes are unidirectional. This can be worked around with a named pipe to redirect the input and output.mkfifo backpipe nc -l 12345 0<backpipe | nc www.google.com 80 1>backpipe
另一个有用的行为是
netcat
用作代理。端口和主机都可以重定向。看这个例子:nc -l 12345 | nc www.google.com 80
端口 12345 代表请求。
这会
nc
在端口 12345 上启动一个服务器,并且所有连接都被重定向到google.com:80
. 如果 Web 浏览器向 发出请求nc
,该请求将发送到 google,但不会将响应发送到 Web 浏览器。那是因为管道是单向的。这可以通过命名管道来解决,以重定向输入和输出。mkfifo backpipe nc -l 12345 0<backpipe | nc www.google.com 80 1>backpipe
回答by Khaled
Here are the commands:
以下是命令:
$ mkfifo named_pipe
$ echo "Hi" > named_pipe &
$ cat named_pipe
The first command creates the pipe.
第一个命令创建管道。
The second command writes to the pipe (blocking). The &
puts this into the background so you can continue to type commands in the same shell. It will exit when the FIFO is emptied by the next command.
第二个命令写入管道(阻塞)。将其&
置于后台,以便您可以继续在同一个 shell 中键入命令。当 FIFO 被下一个命令清空时,它将退出。
The last command reads from the pipe.
最后一个命令从管道中读取。
回答by Nicolas Mas
Open two different shells, and leave them side by side. In both, go to the /tmp/
directory:
打开两个不同的外壳,并将它们并排放置。在两者中,转到/tmp/
目录:
cd /tmp/
In the first one type:
在第一种类型中:
mkfifo myPipe
echo "IPC_example_between_two_shells">myPipe
In the second one, type:
在第二个中,键入:
while read line; do echo "What has been passed through the pipe is ${line}"; done<myPipe
First shell won't give you any prompt back until you execute the second part of the code in the second shell. It's because the fifo read and write is blocking.
在您在第二个 shell 中执行代码的第二部分之前,第一个 shell 不会给您任何提示。这是因为 fifo 读写被阻塞了。
You can also have a look at the FIFO type by doing a ls -al myPipe
and see the details of this specific type of file.
您还可以通过执行 als -al myPipe
查看FIFO 类型并查看此特定文件类型的详细信息。
Next step would be to embark the code in a script!
下一步是在脚本中启动代码!
回答by Chaitanya Tetali
Terminal 1:
1号航站楼:
$ mknod new_named_pipe p
$ echo 123 > new_named_pipe
- Terminal 1 created a named pipe.
- It wrote data in it using echo.
- It is blocked as there is no receiving end (as pipes both named and unnamed need receiving and writing ends to it)
- 终端 1 创建了一个命名管道。
- 它使用 echo 在其中写入数据。
- 它被阻塞,因为没有接收端(因为命名和未命名的管道都需要接收和写入端)
Terminal 2:
2 号航站楼:
$ cat new_named_pipe
$ 123
$
- From Terminal 2, a receiving end for the data is added.
- It read the data in it using cat.
- Since both receiving and writing ends are there for the
new_named_pipe
it displays the information and blocking stops
- 从终端2,增加数据的接收端。
- 它使用 cat 读取其中的数据。
- 由于接收端和写入端都在那里,
new_named_pipe
它显示信息和阻塞停止
Named pipes are used everywhere in Linux, most of the char and block files we see during ls -l
command are char and block pipes (All of these reside at /dev).
These pipes can be blocking and non-blocking, and the main advantage is these provides the simplest way for IPC.
命名管道在 Linux 中无处不在,我们在ls -l
命令过程中看到的大部分 char 和 block 文件都是 char 和 block 管道(所有这些都位于 /dev)。这些管道可以是阻塞的和非阻塞的,主要优点是它们为 IPC 提供了最简单的方法。