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

John Khvatov ivaxer at gmail.com
Tue Oct 16 11:12:09 CEST 2012

Hello Jon!

Thank you for your reply.

On Oct 12, 2012, at 11:07 PM, Jon Dyte wrote:

> 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.

I understand it. Sad that zmq api does not allow to release cloned resources in child.

> 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.

Unfortunately this is my  case.

WBR, John Khvatov

More information about the zeromq-dev mailing list