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


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 have the fd resource?


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.

Answered By – user25148

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published