1
我試圖製作一個簡單的用戶空間程序,它在讀取文件時動態生成文件內容,非常像虛擬文件系統。我知道有像FUSE這樣的程序,但是對於我想要做的事情他們似乎有點沉重。動態生成文件內容(窮人的proc文件)
例如,一個簡單的計數器實現會是什麼樣子:
$ 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?