Bush
Bush

Reputation: 2533

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?

Upvotes: 36

Views: 31009

Answers (1)

user25148
user25148

Reputation:

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.

Upvotes: 28

Related Questions