<HTML ><HEAD ><TITLE >Process Control Functions</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Function Reference" HREF="funcref.html"><LINK REL="PREVIOUS" TITLE="pg_update" HREF="function.pg-update.html"><LINK REL="NEXT" TITLE="pcntl_exec" HREF="function.pcntl-exec.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="reference" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.pg-update.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.pcntl-exec.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.pcntl" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >LXXXII. Process Control Functions</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN78107" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="pcntl.intro" ></A >Introduction</H1 ><P > Process Control support in PHP implements the Unix style of process creation, program execution, signal handling and process termination. Process Control should not be enabled within a webserver environment and unexpected results may happen if any Process Control functions are used within a webserver environment. </P ><P > This documentation is intended to explain the general usage of each of the Process Control functions. For detailed information about Unix process control you are encouraged to consult your systems documentation including fork(2), waitpid(2) and signal(2) or a comprehensive reference such as Advanced Programming in the UNIX Environment by W. Richard Stevens (Addison-Wesley). </P ><P > PCNTL now uses ticks as the signal handle callback mechanism, which is much faster than the previous mechanism. This change follows the same semantics as using "user ticks". You use the <B CLASS="function" >declare()</B > statement to specify the locations in your program where callbacks are allowed to occur. This allows you to minimize the overhead of handling asynchronous events. In the past, compiling PHP with pcntl enabled would always incur this overhead, whether or not your script actually used pcntl. </P ><P > There is one adjustment that all pcntl scripts prior to PHP 4.3.0 must make for them to work which is to either to use <B CLASS="function" >declare()</B > on a section where you wish to allow callbacks or to just enable it across the entire script using the new global syntax of <B CLASS="function" >declare()</B >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B >This extension is not available on Windows platforms.</P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="pcntl.requirements" ></A >Requirements</H1 ><P >No external libraries are needed to build this extension.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="pcntl.installation" ></A >Installation</H1 ><P > Process Control support in PHP is not enabled by default. You have to compile the CGI or CLI version of PHP with <TT CLASS="option" >--enable-pcntl</TT > configuration option when compiling PHP to enable Process Control support. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Currently, this module will not function on non-Unix platforms (Windows). </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="pcntl.configuration" ></A >Runtime Configuration</H1 ><P >This extension has no configuration directives defined in <TT CLASS="filename" >php.ini</TT >.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="pcntl.resources" ></A >Resource Types</H1 ><P >This extension has no resource types defined.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="pcntl.constants" ></A >Predefined Constants</H1 ><P > The following list of signals are supported by the Process Control functions. Please see your systems signal(7) man page for details of the default behavior of these signals. </P ><P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="constant" ><B >WNOHANG</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >WUNTRACED</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIG_IGN</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIG_DFL</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIG_ERR</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGHUP</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGINT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGQUIT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGILL</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGTRAP</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGABRT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGIOT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGBUS</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGFPE</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGKILL</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGUSR1</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGSEGV</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGUSR2</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGPIPE</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGALRM</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGTERM</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGSTKFLT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGCLD</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGCHLD</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGCONT</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGSTOP</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGTSTP</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGTTIN</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGTTOU</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGURG</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGXCPU</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGXFSZ</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGVTALRM</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGPROF</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGWINCH</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGPOLL</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGIO</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGPWR</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGSYS</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ><DT ><TT CLASS="constant" ><B >SIGBABY</B ></TT > (<A HREF="language.types.integer.html" >integer</A >)</DT ><DD ><P > </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="pcntl.example" ></A >Examples</H1 ><P > This example forks off a daemon process with a signal handler. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN78382" ></A ><P ><B >Example 1. Process Control Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php declare(ticks=1); $pid = pcntl_fork(); if ($pid == -1) { die("could not fork"); } else if ($pid) { exit(); // we are the parent } else { // we are the child } // detatch from the controlling terminal if (!posix_setsid()) { die("could not detach from terminal"); } // setup signal handlers pcntl_signal(SIGTERM, "sig_handler"); pcntl_signal(SIGHUP, "sig_handler"); // loop forever performing tasks while(1) { // do something interesting here } function sig_handler($signo) { switch($signo) { case SIGTERM: // handle shutdown tasks exit; break; case SIGHUP: // handle restart tasks break; default: // handle all other signals } } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="pcntl.seealso" ></A >See Also</H1 ><P > A look at the section about <A HREF="ref.posix.html" >POSIX functions</A > may be useful. </P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="function.pcntl-exec.html" >pcntl_exec</A > -- Executes specified program in current process space </DT ><DT ><A HREF="function.pcntl-fork.html" >pcntl_fork</A > -- Forks the currently running process</DT ><DT ><A HREF="function.pcntl-signal.html" >pcntl_signal</A > -- Installs a signal handler</DT ><DT ><A HREF="function.pcntl-waitpid.html" >pcntl_waitpid</A > -- Waits on or returns the status of a forked child</DT ><DT ><A HREF="function.pcntl-wexitstatus.html" >pcntl_wexitstatus</A > -- Returns the return code of a terminated child </DT ><DT ><A HREF="function.pcntl-wifexited.html" >pcntl_wifexited</A > -- Returns <TT CLASS="constant" ><B >TRUE</B ></TT > if status code represents a successful exit </DT ><DT ><A HREF="function.pcntl-wifsignaled.html" >pcntl_wifsignaled</A > -- Returns <TT CLASS="constant" ><B >TRUE</B ></TT > if status code represents a termination due to a signal </DT ><DT ><A HREF="function.pcntl-wifstopped.html" >pcntl_wifstopped</A > -- Returns <TT CLASS="constant" ><B >TRUE</B ></TT > if child process is currently stopped </DT ><DT ><A HREF="function.pcntl-wstopsig.html" >pcntl_wstopsig</A > -- Returns the signal which caused the child to stop </DT ><DT ><A HREF="function.pcntl-wtermsig.html" >pcntl_wtermsig</A > -- Returns the signal which caused the child to terminate </DT ></DL ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.pg-update.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.pcntl-exec.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >pg_update</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="funcref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >pcntl_exec</TD ></TR ></TABLE ></DIV ></BODY ></HTML >