[英]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.