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

Christian Heimes lists at cheimes.de
Thu Jun 21 14:10:43 CEST 2012


Am 20.06.2012 17:04, schrieb Ivan Pechorin:
> 2012/6/20 Christian Heimes <lists at cheimes.de <mailto:lists at cheimes.de>>
> 
>     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
> 
> 
> Sounds too complex: why not just close() all the FDs >= 3 after fork(),
> before exec*() ?
> 
> Linux and all major types of UNIXes have efficient way to do so (without
> iterating up to resource limit).

That approach may (and in the case of a forking webserver will) close
too may file descriptors. Programs use pipes to communicate with their
children or share sockets.

A forking web server may be implemented as:

conn = accept(listening_socket)
child_pid = fork()
if child_pid == 0:
    # child process
    do_work(conn)
    close(conn)
else:
    # server process
    add_to_waiters(child_pid)
    close(conn)

If you closefrom(3) before do_work() then you also close the
communication channel with the client.

Christian



More information about the zeromq-dev mailing list