Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

fork

Creates a child process by duplicating the current process.

Calls POSIX fork(2) to create a new child process. Both parent and child continue execution from the point of the fork call.

Returns:

  • undef on failure (fork could not be performed)
  • 0 in the child process
  • The child’s PID (positive integer) in the parent process

Synopsis

my $pid = fork();
die "fork failed: $!" unless defined $pid;
if ($pid == 0) {
    # child process
    exit(0);
} else {
    # parent process, $pid is child's PID
    waitpid($pid, 0);
}

See Also

exec, wait, waitpid, exit