在 Linux shell (Bash) 中使用命名管道的示例
有人可以发布一个在 Linux 上的 Bash 中使用命名管道的简单示例吗?
Can someone post a simple example of using named pipes in Bash on Linux?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
命名管道实际使用的最佳示例之一...
来自 http://en.wikipedia。 org/wiki/Netcat:
One of the best examples of a practical use of a named pipe...
From http://en.wikipedia.org/wiki/Netcat:
以下是命令:
第一个命令创建管道。
第二个命令写入管道(阻塞)。
&
将其置于后台,以便您可以继续在同一 shell 中键入命令。当下一个命令清空 FIFO 时,它将退出。最后一个命令从管道读取。
Here are the commands:
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.The last command reads from the pipe.
打开两个不同的贝壳,并将它们并排放置。在这两个目录中,都转到
/tmp/
目录:在第一个目录中键入:
在第二个目录中键入: 在
执行代码的第二部分之前,第一个 shell 不会给您任何提示在第二个外壳中。这是因为fifo读写是阻塞的。
您还可以通过执行
ls -al myPipe
查看 FIFO 类型,并查看此特定类型文件的详细信息。下一步是将代码写入脚本中!
Open two different shells, and leave them side by side. In both, go to the
/tmp/
directory:In the first one type:
In the second one, type:
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.
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.Next step would be to embark the code in a script!
创建命名管道
在类 Unix 上,命名管道 (FIFO) 是一种没有内容的特殊类型的文件。
mkfifo
命令在文件系统上创建管道(为其分配名称),但不打开它。您需要像任何其他文件一样单独打开和关闭它。使用命名管道
当您需要从/到多个进程进行管道传输或者无法使用匿名管道连接两个进程时,命名管道非常有用。它们可以通过多种方式使用:
与另一个进程并行:
这里,编写器与读取器一起运行,允许进程之间进行实时通信。
按文件描述符顺序:
事实上,通过管道的通信可以是连续的,但限制为 64 KB(缓冲区大小).
最好使用描述符按顺序传输多个数据块以减少开销。
有条件地带有信号:
FD 允许在 shell 准备好接收数据之前开始数据传输。顺序使用时必需。
该信号应在数据之前发送,以防止管道缓冲区填满时发生死锁。
销毁命名管道
当管道的所有描述符都关闭时,管道本身(及其内容)就会被销毁。剩下的只是一个名字。
要使管道匿名并且在给定名称下不可用(可以在管道仍然打开时完成),您可以使用
rm
控制台命令(它与mkfifo
相反)命令):Creating a named pipe
On Unix-likes named pipe (FIFO) is a special type of file with no content. The
mkfifo
command creates the pipe on a file system (assigns a name to it), but doesn't open it. You need to open and close it separately like any other file.Using a named pipe
Named pipes are useful when you need to pipe from/to multiple processes or if you can't connect two processes with an anonymous pipe. They can be used in multiple ways:
In parallel with another process:
Here writer runs along the reader allowing real-time communication between processes.
Sequentially with file descriptors:
In fact, communication through a pipe can be sequential, but it's limited to 64 KB (buffer size).
It's preferable to use descriptors to transfer multiple blocks of data in order to reduce overhead.
Conditionally with signals:
FD allows data transfer to start before the shell is ready to receive it. Required when used sequentially.
The signal should be sent before data to prevent a deadlock if pipe buffer will fill up.
Destroying a named pipe
The pipe itself (and its content) gets destroyed when all descriptors to it are closed. What's left is just a name.
To make the pipe anonymous and unavailable under the given name (can be done when the pipe is still open) you could use the
rm
console command (it's the opposite ofmkfifo
command):终端 1:
终端 2:
new_named_pipe
的接收端和写入端都存在,因此它会显示信息并阻止停止命名管道在 Linux 中随处可见,我们在
ls -l< 期间看到的大多数 char 和 block 文件/code> 命令是 char 和 block 管道(所有这些都位于 /dev)。
这些管道可以是阻塞的和非阻塞的,主要优点是它们为IPC提供了最简单的方法。
Terminal 1:
Terminal 2:
new_named_pipe
it displays the information and blocking stopsNamed 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.
我想说的是,您可以为管道操作创建后台进程,而不是主cmd。它将对您进行自定义日志处理有很大帮助。
让我们有以下多行命令。
我们需要即时从
make
中 grep 一些东西。很难将 multine 命令替换为${cmd} >log &
(手动或使用 sed 等工具),因此可以将其替换为>log ${cmd}< /code> 相反:
谢谢。
I want to say that you can create background process for pipe operation instead of main cmd. It will help you a lot with custom log processing.
Let we have the following multiline command.
We need to grep something from
make
on the fly. It is hard to replace multine command with${cmd} >log &
(manually or using tools like sed), so you can replace it with>log ${cmd}
instead:Thank you.