rmahin has asked for the wisdom of the Perl Monks concerning the following question:
Hi PerlMonks,
Been working at this one while, and haven't had much luck searching. I have boiled down my code to this little snippet which highlights my issue. Essentially I have threads that pipe the output of other programs to a filehandle but things get weird when I try to remove the files I write to. This is not a problem on any unix machines as far as I can tell but on every version of Windows I have tried the problem persists.
#! perl -l use strict; use warnings; use threads; $|++; sub thread{ my $tid = threads->tid; my $fn = "file" . $tid; open(my $fh, ">", $fn) or die "Couldnt open filehandle $!"; my $pid = open(my $fs, "-|", "perl test.pl") or die "Couldnt open +process: $!"; print $fh $_ while <$fs>; close($fs); close($fh); } my $thr1 = threads->create(\&thread); sleep 1; unlink("file1") or print "Cant remove file1 because $!"; my $thr2 = threads->create(\&thread); sleep 1; unlink("file2") or print "Cant remove file2 because $!"; $thr1->join(); unlink("file1") or print "Still cant remove file1 because $!"; $thr2->join(); unlink("file1") or print "This isnt printed $!"; unlink("file2") or print "This isnt printed $!";
And test.pl is pretty trivial but here ya go. (Yes, that's all that's in there)
sleep 5;
The output is:
Cant remove file1 because Permission denied Cant remove file2 because Permission denied Still cant remove file1 because Permission denied
So my question is how come I still cant remove file1 after that thread has closed the filehandle and the thread has been joined, why does thread 2's presence cause an issue? I'm assuming it has something to do with the fact the processes are both called "perl.exe" and are child processes of the main script but that doesn't help me in solving the issue...Using the utility Unlocker it does show that both of the test.pl processes are keeping file1 and file2 locked, but it doesn't make sense to me as to why that is, or how to fix it. Any help, you guys can give is most appreciated. Thanks!
|
|---|