免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8573 | 回复: 5
打印 上一主题 下一主题

请问close-on-exec的实际意义 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-15 19:07 |只看该作者 |倒序浏览
man fcntl

F_GETFD
              Read  the close-on-exec flag.  If the FD_CLOEXEC bit is 0, the file will remain open across exec, otherwise it
              will be closed.

       F_SETFD
              Set the close-on-exec flag to the value specified by the FD_CLOEXEC bit of arg.

这个,有什么实际的用途吗?因为我实在搞不清楚什么情况下需要设置这个位~~
求赐教

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2007-06-15 19:31 |只看该作者
原帖由 barbas 于 2007-6-15 19:07 发表
man fcntl

F_GETFD
              Read  the close-on-exec flag.  If the FD_CLOEXEC bit is 0, the file will remain open across exec, otherwise it
              will be closed.

       F_SETFD
...

可以参考下这个:
The close-on-exec flag for a file descriptor tells whether the file descriptor should remain open across exec-calls. So when the Tcl interpreter has heaps of files open and the close-on-exec flag is not set, the exec'ed command will inherit all these file descriptors. If the close-on-exec flag is set on some file descriptors, those will be closed on exec, so that the subprocess cannot read/write the open descriptors and cannot keep the file open even if the Tcl interpreter terminates.

This would cause trouble if the file was to be deleted (which physically takes place when the file is no longer open) or - even worse - if the file is a pipe. In this case the other process dealing with the pipe will believe that there is still a reader/writer and won't detect EOF or "no reader present", hence keep waiting.

原文地址
http://athena.wolf.eu.org/~wolf/dupa/wiki.tcl.tk/6030

论坛徽章:
0
3 [报告]
发表于 2007-06-15 19:45 |只看该作者
就是说平时还是不要多事去设F_SETFD啦?
那具体什么时候用啊?给个例子么~~
我看某些日志模块,打开日志文件后会setfd一把,也没想明白有什么必要……

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2007-06-15 20:13 |只看该作者
原帖由 barbas 于 2007-6-15 19:45 发表
就是说平时还是不要多事去设F_SETFD啦?
那具体什么时候用啊?给个例子么~~
我看某些日志模块,打开日志文件后会setfd一把,也没想明白有什么必要……

老实说,我还真没有想到在什么地方非得用 close-on-exec 不可。不过在许多时候用这个很方便罢了,例如说不想让通过 exec 执行的程序继承 pipe 或者临时文件啥的。

论坛徽章:
0
5 [报告]
发表于 2007-06-15 22:28 |只看该作者
嗯,受教,多谢~~

论坛徽章:
0
6 [报告]
发表于 2007-06-16 21:47 |只看该作者
我知道会关闭,我只是很奇怪什么时候需要它关闭而已~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP