You are forking within a loop. Fine, perhaps, except I don't see where the loop's upper limit ( $threads ) is being set. Is it possible that we're not seeing some important piece of code? It is pretty easy to fork-bomb a system with a runaway loop.
Dave
"If I had my life to live over again, I'd be a plumber." -- Albert Einstein