#! 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>; print "[$tid] eof pipe"; close($fs); print "[$tid] pipe closed"; close($fh); sleep 5 if $tid == 2; print "[$tid] thread ending"; } my $thr1 = threads->create(\&thread); sleep 5; unlink("file1") or print __LINE__ . ": Cant remove file1 because $!/$^E"; my $thr2 = threads->create(\&thread); sleep 1; unlink("file2") or print __LINE__ . ": Cant remove file2 because $!/$^E"; $thr1->join(); print __LINE__ . ": Still cant remove file1 because $!/$^E" and Win32::Sleep 500 until unlink("file1"); $thr2->join(); unlink("file1") or print __LINE__ . ": This isnt printed $!/$^E"; unlink("file2") or print __LINE__ . ": This isnt printed $!/$^E"; #### C:\test\junk>..\junk41 26: Cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process 29: Cant remove file2 because Permission denied/The process cannot access the file because it is being used by another process [1] eof pipe [1] pipe closed [1] thread ending 32: Still cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process 32: Still cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process 32: Still cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process 32: Still cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process 32: Still cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process 32: Still cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process 32: Still cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process 32: Still cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process 32: Still cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process 32: Still cant remove file1 because Permission denied/The process cannot access the file because it is being used by another process [2] eof pipe [2] pipe closed [2] thread ending 36: This isnt printed No such file or directory/The system cannot find the file specified