Linux - Perl fork loop without exiting child process
<p>I'm new to fork concept. I'm trying a simple script with fork loop expecting to execute twice but end up with 3 occurrence. is it because i did not exit the child process ? can you clarify please  </p>

<p>script :</p>

<pre><code>#!/usr/bin/perl

open(my $fh, '&gt;', 'report.txt');
print $fh "before invoke fork \n";
close $fh;

@list = (1, 2);

foreach $a (@list){

    if($pid = fork){
       open(my $fh, '&gt;&gt;', 'report.txt');
       print $fh "Parent process! $a $pid\n";
       close $fh;

    }else {
       open(my $fh, '&gt;&gt;', 'report.txt');
       print $fh "Child process $a $pid\n";
       close $fh;
    }
}
</code></pre>

<p>output: </p>

<p>before invoke fork
My second report generated by perl 1 1808
My second report generated by perl 2 1809
My thrid report generated by perl 2 0
My thrid report generated by perl 1 0
My second report generated by perl 2 1810
My thrid report generated by perl 2 0</p>