C 中的多个管道
我想在 c 中实现多管道,所以我可以做这样的事情,其中 |||
表示将标准输入复制到 N 个管道命令):
cat /tmp/test.log ||| wc -l ||| grep 测试1 ||| grep 测试2 | grep test3
这将返回文件中的行数和文件中包含“test1”字符串的行和文件中的行包含“test2”&& 'test3' string
换句话说,这将产生这 3 个常规管道的效果:
cat /tmp/test.log | wc -l --> stdout
| grep test1 --> stdout
| grep test2 | grep test3 --> stdout
有人已经实现了这样的东西吗?我没有发现任何东西... 注意:我知道这可以使用脚本语言或 bash 多个文件描述符来完成,但我正在搜索 C 代码来完成它。
谢谢!
I want to implement multi pipes in c so I can do something like this, where |||
means duplicate the stdin to N pipe commands):
cat /tmp/test.log ||| wc -l ||| grep test1 ||| grep test2 | grep test3
This will return to me the number of lines in the file and the lines in the file that contain 'test1' string and the lines in the file that contain 'test2' && 'test3' string
In other words this would have the effect of these 3 regular pipelines:
cat /tmp/test.log | wc -l --> stdout
| grep test1 --> stdout
| grep test2 | grep test3 --> stdout
Has someone already implementated something like this? I didn't find anything...
NOTE: I know it can be done with scripting languages or with bash multiple file descriptors, but I am searching C code to do it.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许你应该从
tee
命令和 检查他们的代码。Maybe your should start off with the
tee
command and examine their code.因为在 C 中不可能有多个进程(或线程)读取相同的文件描述符而不耗尽读取的数据,所以所有解决方案都必须将读取的数据存储在临时文件中,然后每个读取临时文件。
Because it is impossible in C to have more than one process (or thread) read the same file descriptor without draining the data read, all solutions will have to store the data read in a temporary file and then each read the temp file.