|
From: Tor L. <tm...@ik...> - 2008-03-07 17:02:04
|
> f = fopen("junk.txt", "r");
> p = popen("child.exe", "r");
The problem is that here the C file descriptor for f (and more
importantly the underlying Win32 file HANDLE for it) is inherited by
the child process.
> fclose(f);
Closing the file in the parent doesn't close it in the child.
> err = unlink("junk.txt");
As the file is open in the child process, deleting it will fail. You
cannot delete an open file on Windows unless the right to delete it
while open was specified in the CreateFile() call that opened the
file. The Microsoft C runtime doesn't do that for some reason. Read
the MSDN documenation for CreateFile(), and the DELETE bit of the
"standard access rights" and the FILE_SHARE_DELETE bit of the "share
mode".
What you need to do is prevent the file HANDLE for the file from being
inherited by the child process. If you use open() to open the file
instead of fopen(), you can add the O_NOINHERIT flag to the open
flags.
If you for some reason do want the file HANDLE (and C file descriptor)
to be inherited, you could also use the lower level CreateFile() to
open the file with delete access (and possibly even allowing other
processes to delete it while it is open), and then create a C file
descriptor for the file HANDLE by using _open_osfhandle.
Yes, this is totally unlike Unix, but then nobody claimed Windows is like Unix.
--tml
|