When should I use O_CLOEXEC when I open file in Linux?

六眼飞鱼酱① 提交于 2021-02-07 11:16:17

问题


My process forks several times, and each time the child will exec - means I want it to run some other program.

In the main process I open a file descriptor with the open() syscall.

Would it be correct to give it a flag O_CLOEXEC so the new program that I run with exec() wouldn't has the fd resource?


回答1:


Yes, unless you need the program you exec to have access to that file descriptor. You can also close the file descriptor manually in the child process before calling exec, but that's more error prone.



来源:https://stackoverflow.com/questions/15447657/when-should-i-use-o-cloexec-when-i-open-file-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!