加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux proc / pid / fd for stdout是11?

发布时间:2020-12-13 19:48:46 所属栏目:Linux 来源:网络整理
导读:执行stdout重定向到文件的脚本.所以/ proc / $$/ fd / 1应指向该文件(因为stdout fileno为1).但是,文件的实际fd是11.请解释,为什么. 这是会议: $cat hello.sh#!/bin/sh -els -l /proc/$$/fd 2$./hello.sh /tmp/1total 0lrwx------ 1 nga users 64 May 28 22
执行stdout重定向到文件的脚本.所以/ proc / $$/ fd / 1应指向该文件(因为stdout fileno为1).但是,文件的实际fd是11.请解释,为什么.

这是会议:

$cat hello.sh
#!/bin/sh -e
ls -l /proc/$$/fd >&2

$./hello.sh > /tmp/1
total 0
lrwx------ 1 nga users 64 May 28 22:05 0 -> /dev/pts/0
lrwx------ 1 nga users 64 May 28 22:05 1 -> /dev/pts/0
lr-x------ 1 nga users 64 May 28 22:05 10 -> /home/me/hello.sh
l-wx------ 1 nga users 64 May 28 22:05 11 -> /tmp/1
lrwx------ 1 nga users 64 May 28 22:05 2 -> /dev/pts/0

解决方法

我怀疑,但这在很大程度上取决于你的shell的行为方式.您看到的文件描述符是:

> 0:标准输入
> 1:标准输出
> 2:标准错误
> 10:运行脚本
> 11:脚本正常标准输出的备份副本

描述符10和11在exec上是关闭的,因此不会出现在ls进程中.然而,在分叉之前为0s准备了0-2.我在破折号(Debian Almquist shell)中看到了这种行为,但在bash(Bourne再次shell)中却没有. Bash反而在分叉后执行文件描述符操作,并且顺便使用255而不是10用于脚本.在分叉后执行更改意味着它不必恢复父级中的描述符,因此它没有备份副本到dup2.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读