[omniORB] RE: Fork/Exec

Sandro Tolaini tolaini@libero.it
Tue, 27 Feb 2001 09:15:33 +0100


> I have the similar problem like yours. I also want to do the fork/execl in
a
> method and continue to get all sorts of problems. If you resolve your
> problems, can I share some of your experience? How do you workaound it. I
am
> using HP-UX 11.0

It seems that the only way to fork/exec successfully (at least with Linux
and FreeBSD) is to manually close all the open files (except for fd 0 (cin),
1 (cout) and 2 (cerr)) after fork() but before execv(). Something like:

---------------------------------------
pid_t pid = fork()

if ( pid == 0 )
{
    setsid();
    int maxfd = getdtablesize();
    for ( int fd = 3; fd < fdmax; fd++ )
    {
        close( fd );
    }
}

execv( exe, args );
---------------------------------------

Under Linux you can close only the file descriptors that are currently open
by looking at /proc/self/fd directory, under which you'll find the opened
files.

This is really an hack, and could not work under other OS. I know there are
some real issues with forking multithreaded programs, so the best way to do
this would be to use named pipes and an early fork before ORB initialization
to create a "spawner" process, as Duncan suggested.

Cheers,
  Sandro Tolaini