Linux: how to mark a file descriptor as not inheritable on fork? [duplicate]
Is it possible to mark a specific file descriptor as not inheritable, or close it, in the child process when fork()
is invoked?
No. All file descriptors are inherited in fork. You can set a fd to be closed on exec, however, by using fcntl(fd, F_SETFD, FD_CLOEXEC)
.
No its not possible. By default child processes with inherit file table from parent process.
If you really want close-on-fork, something like this could work:
static void fd_to_close;
static void closer()
{
close(fd_to_close);
}
pthread_atfork(0, 0, closer);
Normally close-on-exec is the desired behavior anyway, though.
精彩评论