Thanks for all of the answers.. I do not want to use Parallel::ForkManager because I am not quite sure how to ship it with my script (this is a homework, but do not worry, I am doing an "extra" feature in the homework so it is okay (I can run 20 children processes at the same time, no one cares, but i want to do ~4 by ~4 when possible).
I am just not sure how to fork ~4 different processes. Below is the code I just wrote..
while(scalar(@directories) > 0) {
#Take first 4 when there are 4 or more elements in the array
if(scalar(@directories)>=4){
@part= @directories[0..3];
}
#Take as many as possible when <4 elements in the array
elsif(scalar(@directories)<4){
@part = @directories [0..scalar(@directories)];
}
#Shorten the array by the number of elements taken in @part
for(my $i=0; $i<scalar(@part); $i++) {
shift(@directories);
}
#For each element in @part, create children processes
#************************************
foreach my $f (@part) {
my $secdirtoget= "${passed_dir}/StretchingDecaAlanine/GMXCubic
+Box/Umbrella/${f}";
chdir ($secdirtoget);
system ("ln -s ../md_umbrella.mdp ../index.ndx ../topol.top ..
+/posre_CTerm.itp ../posre.itp .");
system ("grompp -f md_umbrella.mdp -c pullconf.gro -n index.nd
+x");
exec 'mdrun', '>logje 2>&1 &';
chdir ("${passed_dir}/StretchingDecaAlanine/GMXCubicBox/Umbrel
+la");
}
#************************************
}
The thing is that I want my script to wait until 4 or less mdrun calls in the
foreach my $f (@part) finish doing their job, and then continue with the next 4 or less elements when a new partition in the beginning of the
while is made..
Many thanks..