仮想ファイルシステムのように、ファイルの読み取り時にファイルの内容を動的に生成する単純なユーザー空間プログラムを作成しようとしています。FUSE のようなプログラムがあることは知っていますが、私がやりたいことに対しては少し重いようです。
たとえば、単純なカウンターの実装は次のようになります。
$ cat specialFile
0
$ cat specialFile
1
$ cat specialFile
2
名前付きパイプではないかと思っspecialFile
ていましたが、あまり運がありませんでした。ここでも役立つかもしれないと思っselect
ていましたが、どのように使用するかわかりません。基本的な概念が欠けていますか?
#include <stdio.h>
int main(void)
{
char stdoutEmpty;
char counter;
while (1) {
if (stdoutEmpty = feof(stdout)) { // stdout is never EOF (empty)?
printf("%d\n", counter++);
fflush(stdout);
}
}
return 0;
}
次に、使用法は次のようになります。
shell 1 $ mkfifo testing
shell 1 $ ./main > testing
shell 2 $ cat testing
# should be 0?
shell 2 $ cat testing
# should be 1?