2 ms·
fork() also clones all the filedescriptors that might be open to the child. It is the resposibility of the forking code to close filedescriptors it doesn't need
by bluejekyll 9y ago
fork() also clones all the filedescriptors that might be open to the child. It is the resposibility of the forking code to close filedescriptors it doesn't need. Easy way to cause too many open files errors.
(I read this article quickly, so apologies if I missed it.)
And then there's the fun of dealing with stdout, and stderr at the same time; in a single thread.
One more fun thing to do with fork is to learn to pass ownership of the child process to pid 1, what the command nohup does. A parent process when it exits, requires that all child processes exit as well.
- geocar 9y ago> fork() also clones all the filedescriptors that might be open to the child. ... Easy way to cause too many open files errors. fork() cannot fail with EMFILE or ENFILE[1] [1]: http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html#tag_16_156_05 http://pubs.opengroup.org/onlinepubs/9699919799/functions/fo... > It is the resposibility of the forking code to close filedescriptors it doesn't need. FD_CLOEXEC can cause exec() to close file descriptors automatically. > A parent process when it exits, requires that all child processes exit as well. This is not true
- bluejekyll 9y ago> fork() cannot fail with EMFILE or ENFILE[1] This is true. But if you believe you're only closing files or sockets in the child process, then running into this issue is very simple. > > A parent process when it exits, requires that all child processes exit as well. > This is not true Sorry, you're right I was thinking of the SIGHUP to the child, it doesn't actually require an exit.