关于/dev/fd/

来源:互联网 发布:电脑有声阅读软件 编辑:程序博客网 时间:2024/05/21 04:01

原文地址:  https://discussions.apple.com/thread/2785618?tstart=0


提问者对于"WARNING: Can't open file /dev/fd/3: Permission denied" 有担心


Apple用户的回答:

It's nothing to worry about, it's actually a little bit of developer code to find the file descriptor of an application, not something a user needs to read or write so just forget about it.

More: +To understand what is going on, first note that "/dev/fd" is a "virtual" thing. It is a kind of a hack (developed for the use of shell scripts, I think) that gives a program a view of *its own* file descriptors.+

+This means that if I have two programs running (e.g., '/bin/bash' and '/usr/bin/du'), and each of those looks in "/dev/fd", they could see different things. Each will see one file for each file descriptor that it has open, and the name of the file will be the number of that open descriptor.+

+For example, by default, each program will have three open file descriptors, 0, 1, 2 (stdin, stdout, stderr). Different programs will have different ones open, so what you saw above is the result of that. The shell looked in "/dev/fd" and saw a number of files, one for each of its open files. The names were then passed to 'du' on the command line, as if the command were+
+du -ks /dev/fd/0 /dev/fd/1 /dev/fd/2 /dev/fd/3 ...+

+Then 'du' runs. Without looking at the code, I can only guess, but what has to be happening is that 'du' has closed FD 3, and when it tries to get information about '/dev/fd/3', the kernel gives it an error. The error is "EBADF" (see '/usr/include/sys/errno.h'), because access to "/dev/fd/3" is the same as accessing file descriptor 3 (for example, stat("/dev/fd/3",...) is the same as fstat(3,...))."+


就我目前的水平,总结一下这里面说到的几个知识点:

1./dev/fd是虚拟的,用来提供一个程序program所看到的文件描述符(不同的program,即使是同一个用户的,看到的也不同)

2. 默认的文件描述符有0: stdin;1:stdout;2:stderror

3. 当program已经关闭文件描述符3之后还要尝试去读或者写时,会报permission denied

0 0
原创粉丝点击