[zeromq-dev] zmq_close() has no effect after fork()

Jon Dyte jon at totient.co.uk
Fri Oct 12 21:07:51 CEST 2012


On 12/10/12 09:33, John Khvatov wrote:
> Hello Pieter,
>
> I don't need ZMQ in forked process, but I need forked processes after creating context in parent process. I don't want that the forked process holds ZMQ resources. So, if parent has created context and has retain resources, why it can't release them after fork()?

John

The parent does free the resources. The problem is that some of the os 
supplied resources(ie file descriptors in this case) are 'cloned' by yr 
fork in the child.

Until these are closed, these have the affect of keeping the socket bound.



You could call
getdtablesize() and get the number of  descriptors the process can have 
open, or use OPEN_MAX and just close all the descriptors above 3


if (pid == 0) {
           int i;
           int max_fds;
           if (( max_fds = getdtablesize()) == -1) max_fds = OPEN_MAX;
           for (i = 3 ; i < max_fds ; ++i) close(i);
           /* child 'work' begins here */
           sleep(3600);
         }


BUT this does rely on the fact that other libraries and code(static 
initialisers etc etc) that are part of the initial process havent 
created fd's in the parent that the child will subsequently need.

Jon






More information about the zeromq-dev mailing list