开发者

Linux: how to mark a file descriptor as not inheritable on fork? [duplicate]

开发者 https://www.devze.com 2023-03-18 16:47 出处:网络
This question already has answers here: 开发者_如何学JAVA Prevent file descriptors inheritance during Linux fork
This question already has answers here: 开发者_如何学JAVA Prevent file descriptors inheritance during Linux fork (3 answers) Closed 3 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消