in reply to fork() not working as expected
The while (%pids) block should be outside of the foreach.
Right now you're doing a fork and then waiting for the child to finish before starting the next one.