[zeromq-dev] (py)zmq, fork and cloexec

Christian Heimes lists at cheimes.de
Wed Jun 20 16:32:50 CEST 2012

Am 20.06.2012 14:43, schrieb Ian Barber:
> This is interesting - I wonder if this is somethign that would help the
> situations where people do use fork to manage their children (in PHP
> that happens a fair bit).

No, CLOEXEC doesn't help you with fork() only programs. Just fork() call
doesn't evaluate the CLOEXEC flags (O_CLOEXEC, SOCK_CLOEXEC). Only the
execv() family of function affect FDs with the CLOEXEC flags.

(For the Windows developers and those who are not acquaint with low
level POSIX apis: fork() clones the current process including most data
structures, file descriptors and other resources. The memory isn't
actually copied on most systems as fork() uses copy-on-write. execv()
replaces the current process image with a new binary. fork() + execv()
are used to execute a program. It feels a bit strange that Unix
processes give birth to a child and immediately replace the new born
with a different thing.)

For CPython core we had to introduce an API function that re-initializes
several data structures after fork, This function must be called as
early as possible in the child after fork(). [1] ZMQ could introduce a
similar API hook. You'd have to register all file descriptors in a
central registry, remove the FDs on close and close() the FDs after
fork(). That's lots of work but C++ should make it easie



More information about the zeromq-dev mailing list