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!


In reply to Multithreaded script keeps files locked when reading output from multiple processes on Windows by rmahin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.