close a filehandle for all processes

H

himanshu.garg

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
 
B

Brian McCauley

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.
 
H

himanshu.garg

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
 
J

Joost Diepenmaat

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top