关于/dev/fd/

原文地址:  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值