foreach my $hostname (@host) { my $thr = fork(); if($thr) { #parent push(@childsall, $thr); next; } else { connecttohost($hostname); exit; # <---- here } } foreach my $child_pid (@childsall) { my $tmp = waitpid($child_pid, 0); }