Recently I have encountered a problem on Windows OS with Perls behaving differently with file locking.
I will try my best to explain the symptoms, please let me know if anything is unclear.
Tested with the following combinations (Windows Server 2003 and Windows XP):
1. Apache with CygwinPerl
2. Apache with ActivePerl
3. IIS 6 with CygwinPerl
4. IIS 6 with ActivePerl
Basic symptom:
With test 2, 3 and 4, when Perl opens a file handle to file[X], in the same process, none of the external executables are able to access file[X].
However, with test 1, even though the handle is still opened, the external executables can still access the file.
The test code for ActivePerl (can be used under both Apache and IIS 6)
======================================================================
#!C:\\cygwin\\bin\\perl.exe -w
use strict
use Fcntl qw(:DEFAULT :flock);
use CGI;
use CGI:Carp 'fatalsToBrowser';
print STDOUT "Content-type:text/html\n\n";
# Cygwin Path
# my $file_path = "C:/public_html/testing/test_output.txt";
# ActivePerl Path
my $file_path = "C:\\public_html\\testing\\test_output.txt";
# 1. Holding the file handle open
print STDOUT "Holding the file handle open\n";
my $file_handle;
sysopen($file_handle, $file_path, O_RDWR | O_CREAT) || die "ERROR: Could not open $file_path ($!)";
# 2. Write to the file
print STDOUT "Output the testing text file\n";
my $cmd = "echo \"Test for CygwinPerl\" > $file_path";
`$cmd`;
# 3. Close the file handle
print STDOUT "Close the file handle\n";
close($file_handle);
exit(-1);
======================================================================
Finding:
For the tests that can access the file, I got the following error message in the Server log: "The process cannot access the file because it is being used by another process."
I would think it is something to do with Windows file locking, but what I don't understand is why does it work with Apache/Cygwin combination?
If Apache and Cygwin can do, we should be able to do it too...
Is there anyway to keep the file handle open while allowing external executables to access it?
Thanks,
Jeffrey