![](/img/trans.png)
[英]How to use a file pointer of a file descriptor with multiple child processes without getting “Bad file descriptor” error in C?
[英]How do I create a file descriptor in linux that can be read from multiple processes without consuming the data?
我想创建一个文件描述符,将其写入时可以从多个进程中读取而不消耗数据。 我知道命名管道,但是由于它是fifo,因此只有一个进程可以获取数据。
我的用例如下。 在git中,钩子使用stdin将要处理的数据传递到钩子中。 我希望能够从父钩子调用多个子钩子。 每个子钩子应获得与父级接收到的相同的stdin数据。 如果在使用管道时没有记错,那么每个子进程将不会获得相同的stdin。 相反,第一个读取stdin的钩子将消耗数据。 这个对吗?
我目前唯一真正可行的选择是将stdin写入文件,然后从每个子进程读取该文件。 还有另一种方法吗?
也许您可以尝试使用tee。
从三通:
tee - read from standard input and write to standard output and files
在这种情况下,使用以下方式处理: tee >(parent) >(hook1) >(hook2) >(hookn)
(每个钩子是一个不同的进程,命令,可根据需要使用shell)
这里是一个例子:
#!/bin/bash
while read stdinstream
do
echo -n ${stdinstream} | tee >(parent) >(hook2) >(hook1)
done
编辑:
在您的情况下,我认为您不需要while循环,这样就足够了:
read stdinstream
echo -n ${stdinstream} | tee >(parent) >(hook2) >(hook1)
希望这会对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.