on Fri, 21 Jun 2002 06:30:49 GMT, [EMAIL PROTECTED] (Langa
Kentane) wrote: 

> I wish to use the fork() function on one of my scripts. I would
> like more clarity on the way it works.
> 
> Take for instance the ff code:
> 
> Sub mysub
> {
>      while (<THATFILE>)
>      If ($_ eq "SCANME")
>      {
>           fork()
>           system("nessus" "thathost");
>           print "Forked!\n";
>      }
>      Else
>      {
>           fork()
>           system("nmap", "-sS", "-T", "insane", "mynetwork")
>           print "Forked on else\n";
>      }
>      close THATFILE;
> }
> 
> What I would like to know when I do the fork on the if statement,
> does the child just do the stuff under if block & exit?? 
> What happens exactly? Is this the right way of doing this?

That's not the way it works. In a nutshell and without being complete 
(see perldoc -f fork and perldoc perlipc for more information), this 
is what happens:

When you call fork, an exact copy is made of your running program, 
and the execution of both programs continues right after the fork 
call. The difference between the two programs is in the return value 
of the fork call. In the child, fork returns '0', in the parent it 
returns the pid of the child (which is a positive integer), if the 
fork fails, it returns undef:

    my $pid = fork();
    die "Couldn't fork: $!" unless defined($pid);
    if ($pid == 0) {
        # child code goes here
    } else {
        # parent code goes here
    }

-- 
felix


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

Reply via email to