My Fork() don't Work!

Asparagus

Senior member
Aug 16, 2001
284
1
81
Newbie to Forking here...

I need to create a simple program that runs on Linux (Fedora Core 2, to be exact) that has a parent process that spawns 9 child processes. The child processes need to hang around for about 10 seconds or so (long enough to see all of the processes running when you type "ps" command). Then, the child processes need to die and then finally the parent. (So tragic!)

I thought that putting the fork calls inside of a FOR loop would do it for me, but it doesn't. A whole lot more than 9 child processes are being created. The code is below...what am I doing wrong???

Thanks in advance...
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
You need to put a test around your forking loop (sorry - it just had to be said that way :p ) to check whether the current process is the parent or not. As it is, your parent spawns 9 processes, and each of these nine processes spawns 9 more ... ad-infiniteum. You've just built a nice fork-bomb.