FD Inhertiance on fork()

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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....
    4. 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...
    5. 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...
  3. #2

    Default Re: FD Inhertiance on fork()

    Peteris Krumins <pkruminsREMOVETHIS@inbox.lv> wrote:
    <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?
    <snip>

    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

  4. #3

    Default 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

  5. #4

    Default Re: FD Inhertiance on fork()

    [email]phil-news-nospam@ipal.net[/email] wrote in news:bfl1vb0hl3@enews3.newsguy.com:
    > 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.
    >
    Thanks, it helped. Also William's suggestion was great.


    P.Krumins
    Peteris Krumins Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139