How do I know all processes are finished?

Hi all,

I am writing a C shell script that starts a program. The program forks of several child processes. Only when all child processes are done, I want to archive my log files. Below is what I have so far, but unfortunately it doesn't work.

MyProgram

if (-e processes.txt) then
rm processes.txt
endif
ps -C MyProgram | grep MyProgram > processes.txt

while (-s processes.txt)
rm processes.txt
ps -C MyProgram | grep MyProgram > processes.txt

rm processes.txt

# start archiving log files

The while loop is done twice and than the archiving starts, while one of the child processes is still running. Does anyone know what I am doing wrong or does anyone have any other suggestion on how to do this?
Thanks a lot.

Your best bet is to have each fork generate and then remove a lockfile of their own upon start and exit. The parent process or some other process can then monitor these for status and run the archive process when all of the lockfiles are gone. Something like while [ -e $lockfdir/$lockfile ] or sleep then check an ls command's $? for 0 would probably work ...

I just found it that my solution works when I use goto's instead of while.
Does anyone know why that is?
Thanks.