Chad Kellerman wrote: > Hi, > > I was wondering if someone can shed some light on a problem I am > having. I have a sub routine that forks each variable in an array one > at a time. But after the last element in the array the program exists > from the sub routine, it does not continue after the the last element > in the array is done forking. > > <code> > > sub tarusers { > > foreach my $user (@home) { > #creating fork for each user to free memory when the child dies. > my $pid; > unless ($pid = fork) { > while (fork) { > # in here I have code that tar users on a remote box > and gzips them on l;ocal machine. Plus logs everything to a mysql > db. > exit 0; > } > exit 0; > } > waitpid($pid,0); > } > # start tarring home2 users. > if (@home2) { > foreach my $user2 (@home2) { > my $pid; > unless ($pid = fork) { > while (fork) { > # here I have code that tars users on a > remote box and gzip locally. The @home2 may or may not exists > exit 0; > } > exit 0; > } > waitpid($pid,0); > } > } > } > > </code> >
Chad, you have posted this question a number of times and a number of us had given you some advice on how to debug your script. i am sure you are very confused and really want to get it to work. that's why you keep posting and hope someone will give you an answer to your question. your function is basically just: sub tarusers{ foreach my $user (@users){ my $pid = fork; unless($pid){ #-- tar stuff. do stuff exit; } foreach my $user2 (@home2){ my $id = fork; unless($pid){ #-- tar stuff. do stuff exit; } } } } which is a cleaner version of what you have. you should notice that for each @users, the parents goes into @home2 and run the code inside the inner foreach loop. this means if @users has 500 elements and @home2 has 500 elements, the code will fork at least 500 * 500 times! worst yet, in your original code, you are forking at at least 500 *2 * 500 * 2 times because of you have an extra pair of while(fork) for each foreach loop. i don't understand why you can remove those extra pair of while(fork)??? to me, they are unecessary. the child portion of the while(fork) does absolutely nothing. once it's created, it's exit immedately. david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]