Do files opened by child processes count against the file open limit for the parent process

fileslimitprocess

On Mac or Linux if you use the command ulimit -n you can see the page open limit for what seems to be an individual process according to this stackoverflow post.

So if a parent process spawns child processes and those child processes open files, do those files count against the open file limit for the parent?

Best Answer

The RLIMIT_NOFILE is on the maximum file descriptor value you may obtain/allocate, not on how many files may be open at a time.

Child processes inherit the limit, but other than that there's nothing that a child may do to influence the parent here. If the parent has some free fds in the range 0->limit-1, then it will still be able to open new files (wrt to that limit) regardless of what any of its children does (you may run into other global limits though).

In any case, note that if the limit is say 500, you can still have more than 500 file descriptors open if you had some that were open (including in parent processes) prior to the limit being lowered.

$ bash -c 'exec  1023> /dev/null; ulimit -n 500;
   command exec 600> /dev/null; ls -l /proc/self/fd; exit'
bash: 600: Bad file descriptor
total 0
lrwx------ 1 chazelas chazelas 64 Jun 17 08:40 0 -> /dev/pts/1
lrwx------ 1 chazelas chazelas 64 Jun 17 08:40 1 -> /dev/pts/1
l-wx------ 1 chazelas chazelas 64 Jun 17 08:40 1023 -> /dev/null
lrwx------ 1 chazelas chazelas 64 Jun 17 08:40 2 -> /dev/pts/1
l-wx------ 1 chazelas chazelas 64 Jun 17 08:40 3 -> /dev/null
lr-x------ 1 chazelas chazelas 64 Jun 17 08:40 4 -> /proc/8034/fd

That process running ls has a limit of 500 there inherited from its parent (so can't get a new fd bigger than 499). Still it does have the fd 1023 open.

Related Question