Forking is UNIX lingo for making a new process by dupli-
cating an existing one. The duplicate (child) process inherits code, environ-
ment, memory (copy on write), file descriptors, and everything from the parent
process. Often, you either immediately replace the guts of the process by exe-
cuting another executable program, or close inherited file descriptors and pre-
pare the child process for its job:
-
-
<?php
-
-
$child_pid = pcntl_fork();
-
-
if ($child_pid == -1) {
-
-
-
} else if ($child_pid) {
-
-
-
%d.\n",
-
-
-
} else {
-
-
-
}
-
-
?>
This example demonstrates forking, creating a duplicate of the initial
process. Both processes continue running the current script from the line after
the fork. The difference is that in the parent process, the fork call returned the
process id of the child process, while in the child process the fork call returned
0. This is how you distinguish the creating and created processes.
If pcntl_fork() returns 1, an error occurred and no process was created.
