Accessing an array with child processes in Perl
fork, perl
Solution
Data is not shared amongst processes. Each process has its own copy of it.
If you want to share then the whole wide world of interprocess communication is open to you. The perldoc page perlipc tells you all about it.
If to do not need full processes for each instance then using threads is another method. Unlike processes threads can share data (though they don't by default in Perl). The perldoc page perlthrtut is the starting point for this particular topic.
Problem
Say I have 5 files, named a through e. I want to process these in parallel. I thought I could do something like this: ``` my @ltrs = ('a'..'e'); for my $fnum (0..2) { $pid = fork(); if ($pid) { push(@childs, $pid); } elsif ($pid == 0) { do { my $ltr = shift(@ltrs); print "Open file $ltr on $fnum\n"; } until (scalar(@ltrs)==0); exit(0); } else { die "Couldn't fork: $!\n"; } } foreach (@childs) { waitpid($_, 0); } ``` but each child process is accessing all five elements of `@ltrs`: ``` Open file a on 0 Open file b on 0 Open file c on 0 Open file d on 0 Open file e on 0 Open file a on 1 Open file b on 1 Open file c on 1 Open file d on 1 Open file e on 1 Open file a on 2 Open file b on 2 Open file c on 2 Open file d on 2 Open file e on 2 ``` When one process `shift`s an element from the array, why is that element still there when the next process looks at the array? I assumed that after the first `shift`, whatever process comes along next would find an array that starts with `b`, but clearly I'm missing something.