#! 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";