Distinguish between the parent and child processes : fork « System Functions « Perl






Distinguish between the parent and child processes

      

# If fork returns 0, the current process is the child and not the parent.
# If an error occurs, fork returns a special undefined value called undef.

$pid = fork();

if ($pid == 0) {
   # We're in the child process.

} elsif (! defined $pid) {
   # Not defined: means an error.

} else {
   # Parent process.
}
          

   
    
    
    
    
    
  








Related examples in the same category

1.fork returns the child's process ID
2.Using fork and pipe.
3.Using fork to create child processes
4.To retrieve the process ID for the parent process for your program, call the function getppid.