Unix/Linux中的/dev/fd机制

来源:互联网 发布:阿里云dns解析地址 编辑:程序博客网 时间:2024/05/26 20:23

转自http://blog.csdn.net/xuorui/article/details/5450032

比较新的unix/Linux系统都提供名为/dev/fd的目录,其中有文件0、1、2等文件,打开这些文件,相当于复制这些文件描述符

例如:
fd=open("/dev/fd/0",mode);
等价于
fd=dup(0);
文件描述符fd和0将共享一个文件表记录项。
我们再来做一些试验:
在debian下运行以下命令
#cd /dev/fd
#ls -l
总用量 0
lrwx------  1 root root 64 2005-09-12 22:20 0 -> /dev/pts/0
lrwx------  1 root root 64 2005-09-12 22:20 1 -> /dev/pts/0
lrwx------  1 root root 64 2005-09-12 22:20 2 -> /dev/pts/0
lrwx------  1 root root 64 2005-09-12 22:20 255 -> /dev/pts/0
我们看到在fd目录下,有四个文件0、1、2、255,它们都是指向/dev/pts/0的符号链接
在有一些系统中,提供了/dev/stdin、/dev/stdout、/dev/stderr,它们分别等效于/dev/fd/0、/dev/fd/1、/dev/fd/2
#cd /dev
#ls - l stdin stdout stderr
lrwxrwxrwx  1 root root 4 2005-09-05 07:49 stderr -> fd/2
lrwxrwxrwx  1 root root 4 2005-09-05 07:49 stdin -> fd/0

lrwxrwxrwx  1 root root 4 2005-09-05 07:49 stdout -> fd/1


0 0
原创粉丝点击