ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
3.2.40 forkfork This function does a fork(2) call. If it succeeds, the function returns the child pid to the parent process and 0 to the child process. (If it fails, it returns the undefined value to the parent process. There is no child process.) Note that unflushed buffers remain unflushed in both processes, which means you may need to set $| on one or more filehandles earlier in the program to avoid duplicate output. A nearly bulletproof way to launch a child process while checking for "cannot fork" errors would be: FORK: { if ($pid = fork) { # parent here # child process pid is available in $pid } elsif (defined $pid) { # $pid is zero here if defined # child here # parent process pid is available with getppid } elsif ($! =~ /No more process/) { # EAGAIN, supposedly recoverable fork error sleep 5; redo FORK; } else { # weird fork error die "Can't fork: $!\n"; } } These precautions are not necessary on operations which do an implicit fork(2), such as system, backquotes, or opening a process as a filehandle, because Perl automatically retries a fork on a temporary failure in these cases. Be very careful to end the child code with an exit, or your child may inadvertently leave the conditional and start executing code intended only for the parent process. If you fork your child processes, you'll have to wait on their zombies when they die. See the wait function for examples of doing this. The fork function is unlikely to be implemented on any operating system not resembling UNIX, unless it purports POSIX compliance. |