I have a PERL script that is creating a lot of sockets (around 700 sockets) and then does stuff with those sockets. After that, it uses system(...) command to launch another application which opens even more sockets (like 800 or so) but since the limit on the number of sockets per process is 1024, I exceed the limit and don’t get the expected data from the socket number 1024 and onwards.
Now my question is this:
- Does the system command make the application launched inherit all the open file descriptors/sockets?
- If the answer to the above question is yes, then is there some other way to launch another application such that the launched application does not inherit the file descriptors?
- If the answer to 2. is no, then is there some way to close all inherited file descriptors in the child process?
Take a look at the perlvar $^F which controls which file descriptors are set for close-on-exec. The default setting should be closing all those socket descriptors when you call system(), but if not, perhaps $^F isn’t set to what you want. A very quick and easy way to tell on a Linux system would be to try this call right before your existing system() call:
the output of the ls will show you what files were left open when the “ls” command was executed. Most likely you will see that only stdin, stdout, and stderr (descriptors 0, 1, and 2) are open, but if you see your socket descriptors open, I would try setting $^F = 2 and see if that helps.