Ask a Question related to UNIX Programming, Design and Development.
-
Peteris Krumins #1
FD Inhertiance on fork()
Hello,
I now have the following problem - a project consists of several
programs, one of them is loader which performs checking and loads the
other programs.
The loader has a configuration file which is read, line by line,
checked for a valid directive and an action is taken (function called).
There are some directives which include calling those other programs.
Unfortunately fork() creates a child which inherits file descriptors
from the parent.
Is it possible somehow not to inherit file descriptors from parent on
fork?
- The workaround i have found is to close the configuration file before
fork()-ing, and then open it again and move to the line i stopped. But
this is plain ugly, isn't it?
The problem arises if the configuration file is on a mounted filesystem,
as a new process is fork()-ed it inherits parents fds, and i am unable
to unmount that filesystem anymore.
(I could copy the configuration file elswhere (on a local filesystem),
but this time this is not an option)
P.Krumins
Peteris Krumins Guest
-
CGI Script Fork
I'm working on a cgi script that basically, upon running it, runs a separate script in the background, and displays a message saying "Script is... -
fork not available?
I am running windows 2000 using the PragProgs install. I have tried fork but get an unimplemented on this machine error. I have tried this on... -
fork & wait
T.S.Ravi Shankar wrote: After fork() there are two processes, parent and child. fork returns 0 to the child, and the child's pid to the parent.... -
please help clarify these two fork tests
In article <slrnbhbl0s.bck.drejcica@sonet.utopija.linux>, andrej hocevar <andrejh@volja.net> wrote: In version 1, the parent sleeps for 10... -
Problem with fork
Hi - I am using 'fork' to execute a child process (a perl program) in my CGI. When the user submits the page, I am displaying a status page... -
William Ahern #2
Re: FD Inhertiance on fork()
Peteris Krumins <pkruminsREMOVETHIS@inbox.lv> wrote:
<snip><snip>> Is it possible somehow not to inherit file descriptors from parent on
> fork?
> - The workaround i have found is to close the configuration file before
> fork()-ing, and then open it again and move to the line i stopped. But
> this is plain ugly, isn't it?
you can turn on the F_CLOEXEC flag for the configuration file descriptor
using fcntl(). however, this is for exec*() family of functions, not fork().
or you could just simply close the descriptor in the child. closing it won't
affect the parent's descriptor.
- Bill
William Ahern Guest
-
phil-news-nospam@ipal.net #3
Re: FD Inhertiance on fork()
On 23 Jul 2003 00:14:01 GMT Peteris Krumins <pkruminsREMOVETHIS@inbox.lv> wrote:
| Is it possible somehow not to inherit file descriptors from parent on
| fork?
| - The workaround i have found is to close the configuration file before
| fork()-ing, and then open it again and move to the line i stopped. But
| this is plain ugly, isn't it?
Close it _after_ the fork only in the child process, before doing anything
else. For example:
child_pid = fork();
if ( child_pid == -1 ) goto fork_failed;
if ( child_pid == 0 ) {
/* this is the child process */
close( config_fd );
/* do whatever the child is here to do
...
/* exit or exec another program */
}
/* continue with parent here */
| The problem arises if the configuration file is on a mounted filesystem,
| as a new process is fork()-ed it inherits parents fds, and i am unable
| to unmount that filesystem anymore.
| (I could copy the configuration file elswhere (on a local filesystem),
| but this time this is not an option)
Once the parent is done with the config file (it does NOT need to open it
again since it did not close its descriptor), then it closes the config
file descriptor. All children will have closed it first thing. Then it
is fully closed and the filesystem can be unmounted if other things allow
that.
--
-----------------------------------------------------------------------------
| Phil Howard KA9WGN | [url]http://linuxhomepage.com/[/url] [url]http://ham.org/[/url] |
| (first name) at ipal.net | [url]http://phil.ipal.org/[/url] [url]http://ka9wgn.ham.org/[/url] |
-----------------------------------------------------------------------------
phil-news-nospam@ipal.net Guest
-
Peteris Krumins #4
Re: FD Inhertiance on fork()
[email]phil-news-nospam@ipal.net[/email] wrote in news:bfl1vb0hl3@enews3.newsguy.com:
Thanks, it helped. Also William's suggestion was great.> On 23 Jul 2003 00:14:01 GMT Peteris Krumins
> <pkruminsREMOVETHIS@inbox.lv> wrote:
>
>| Is it possible somehow not to inherit file descriptors from parent on
>| fork?
>| - The workaround i have found is to close the configuration file
>| before fork()-ing, and then open it again and move to the line i
>| stopped. But this is plain ugly, isn't it?
>
> Close it _after_ the fork only in the child process, before doing
> anything else. For example:
>
> child_pid = fork();
> if ( child_pid == -1 ) goto fork_failed;
> if ( child_pid == 0 ) {
> /* this is the child process */
> close( config_fd );
> /* do whatever the child is here to do
> ...
> /* exit or exec another program */
> }
> /* continue with parent here */
>
>
>| The problem arises if the configuration file is on a mounted
>| filesystem, as a new process is fork()-ed it inherits parents fds,
>| and i am unable to unmount that filesystem anymore.
>| (I could copy the configuration file elswhere (on a local
>| filesystem), but this time this is not an option)
>
> Once the parent is done with the config file (it does NOT need to open
> it again since it did not close its descriptor), then it closes the
> config file descriptor. All children will have closed it first thing.
> Then it is fully closed and the filesystem can be unmounted if other
> things allow that.
>
P.Krumins
Peteris Krumins Guest



Reply With Quote

