Chas, thanks a lot for your example.

if the while loop is inside a main infinite loop (as if it was a 
daemon), do I still need to have the waitpid function? and if yes, where 
should be located?

Once again, thanks a lot!


Chas Owens wrote:
> On Tue, 2002-03-26 at 01:14, Ahmed Moustafa wrote:
> Fork returns 0 to the child process and the pid of the child process to
> the parent.  This allows you to say things like
> 
> my @children;
> #loop while get_filename retruns a vaild filename
> #this could easily be a foreach loop of @filenames
> while (my $filename = get_filename()) { 
>       my $forked = fork; #fork into two processes(parent and child)
> 
>       #if $forked is undef then fork failed
>       unless (defined $forked) {
>               warn "something went wrong with fork";
>               next;
>       } 
> 
>       #push the PID of the child onto a stack
>       push @children, $forked; #meaningless for child
>       
>       next if $forked; #parent loops
> 
>       #only the child can get here 
>       encrypt_the_file($filename);
> 
>       #child ends execution here
>       exit 0;
> }
> 
> #reap the children to avoid zombies
> waitpid $_ for (@children);


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to