close a filehandle for all processes

Discussion in 'Perl Misc' started by himanshu.garg@gmail.com, Dec 24, 2007.

  1. Guest

    Hello,

    I have a process that opens a filehandle and after forking a
    child closes the filehandle. The child can be used to run an arbitrary
    command and I cannot do anything there.

    Is there a way I can do a close for all the processes not just
    the parent. Some equivalent of a shutdown for sockets?

    Thank You,
    HG
     
    , Dec 24, 2007
    #1
    1. Advertising

  2. On Dec 24, 8:19 am, wrote:

    > I have a process that opens a filehandle and after forking a
    > child closes the filehandle. The child can be used to run an arbitrary
    > command and I cannot do anything there.
    >
    > Is there a way I can do a close for all the processes not just
    > the parent. Some equivalent of a shutdown for sockets?


    Your question has nothing to do with Perl.

    As far as I know there is no such feature in the POSIX-like standards.

    Note you can close() handles in the child process between the fork()
    and the exec() if you are willing to operate at the lower level.

    You can also set the close-on-exec flag on a file descriptor. See the
    $^F special variable.

    Note that by default all descriptors other than 0,1,2 will be have the
    close-on-exec() property anyhow so this is likely to be an non-issue
    anyhow.
     
    Brian McCauley, Dec 24, 2007
    #2
    1. Advertising

  3. Guest

    On Dec 24, 8:38 pm, Brian McCauley <> wrote:
    > On Dec 24, 8:19 am, wrote:
    >
    > > I have a process that opens a filehandle and after forking a
    > > child closes the filehandle. The child can be used to run an arbitrary
    > > command and I cannot do anything there.

    >
    > > Is there a way I can do a close for all the processes not just
    > > the parent. Some equivalent of a shutdown for sockets?

    >
    > Your question has nothing to do with Perl.


    :) I realized this shortly after sending the original post.

    >
    > As far as I know there is no such feature in the POSIX-like standards.
    >
    > Note you can close() handles in the child process between the fork()
    > and the exec() if you are willing to operate at the lower level.
    >
    > You can also set the close-on-exec flag on a file descriptor. See the
    > $^F special variable.


    The scenario that I am in requires that I close the filehandles only
    when the immediate child has died. Since this is the one execed and it
    can be arbitrary command, close on exec may not work here.

    >
    > Note that by default all descriptors other than 0,1,2 will be have the
    > close-on-exec() property anyhow so this is likely to be an non-issue
    > anyhow.


    I see. This could be useful.

    Thanks,
    HG
     
    , Dec 28, 2007
    #3
  4. writes:

    > On Dec 24, 8:38 pm, Brian McCauley <> wrote:
    >> On Dec 24, 8:19 am, wrote:

    > The scenario that I am in requires that I close the filehandles only
    > when the immediate child has died. Since this is the one execed and it
    > can be arbitrary command, close on exec may not work here.


    So you want to close the file-handle in the parent, when the child
    dies? It will already be closed in the child, since when a process exits
    all its handles are closed.

    In that case you may be able periodically check if the child PID still
    exists, or you can use SIGCHLD or system() or `` etc to get notified
    automatically when the child process ends.

    Joost.
     
    Joost Diepenmaat, Dec 28, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    8,933
    sateeshranga
    Aug 7, 2008
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,203
    David Schwartz
    Dec 9, 2003
  3. Iñaki Baz Castillo
    Replies:
    7
    Views:
    941
    Iñaki Baz Castillo
    Jan 12, 2010
  4. PerlFAQ Server
    Replies:
    0
    Views:
    194
    PerlFAQ Server
    Apr 23, 2011
  5. Replies:
    3
    Views:
    444
    Chung Leong
    Jan 16, 2005
Loading...

Share This Page