子のファイルハンドルにデータを書き込む必要があります。ファイル ハンドルは、フォークする前に親で作成されました。これは、fork がファイル ハンドルを保持し、存在する場合はそれらをロックし、親と子の間で共有されるため、親のファイル ハンドルからデータを読み取ることができるためです。これは、Linux プラットフォームと Windows プラットフォームの両方で動作する親子でデータを共有するためです。Linux で IPC::Shareable を使用してデータ共有を行うことができましたが、Windows では semaphore.pm が利用できないため、これは Windows では機能しません [windos は semaphore.pm をサポートしていません]。私のperlコンパイラをクラッシュさせていました。
したがって、ファイル ハンドル アプローチでは、子で IO 書き込みが発生しません。以下のコードを見てください
use strict;
use warnings;
print "creating file\n";
open FH, ">testfile.txt" or die "cant open file: $!\n";
close FH;
my $pid = fork();
if ( $pid == 0 )
{
print "entering in to child and opening file for write\n";
open FH, ">>testfile.txt" or die "cant open file: $!\n";
print FH "dummy data\n";
print FH "dummy data\n";
print "child sleeping for 5 sec before exiting\n";
sleep 50;
exit;
}
else
{
print "entering the parent process\n";
open FH, "<testfile.txt" or die "cant open file: $!\n";
print <FH>;
print <FH>;
}