pcntl.examples.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 37 行
HTML
37 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Examples</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pcntl.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.pcntl.html">PCNTL Functions</a></div> <div class="up"><a href="book.pcntl.html">PCNTL</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Examples</h1> <div id="pcntl.example" class="section"> <p class="para"> This example forks off a daemon process with a signal handler. </p> <div class="example"> <p><b>Example #1 Process Control Example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">ticks</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$pid </span><span style="color: #007700">= </span><span style="color: #0000BB">pcntl_fork</span><span style="color: #007700">();<br />if (</span><span style="color: #0000BB">$pid </span><span style="color: #007700">== -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> die(</span><span style="color: #DD0000">"could not fork"</span><span style="color: #007700">); <br />} else if (</span><span style="color: #0000BB">$pid</span><span style="color: #007700">) {<br /> exit(); </span><span style="color: #FF8000">// we are the parent <br /></span><span style="color: #007700">} else {<br /> </span><span style="color: #FF8000">// we are the child<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #FF8000">// detatch from the controlling terminal<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">posix_setsid</span><span style="color: #007700">() == -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> die(</span><span style="color: #DD0000">"could not detach from terminal"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">// setup signal handlers<br /></span><span style="color: #0000BB">pcntl_signal</span><span style="color: #007700">(</span><span style="color: #0000BB">SIGTERM</span><span style="color: #007700">, </span><span style="color: #DD0000">"sig_handler"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">pcntl_signal</span><span style="color: #007700">(</span><span style="color: #0000BB">SIGHUP</span><span style="color: #007700">, </span><span style="color: #DD0000">"sig_handler"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// loop forever performing tasks<br /></span><span style="color: #007700">while (</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /><br /> </span><span style="color: #FF8000">// do something interesting here<br /><br /></span><span style="color: #007700">}<br /><br />function </span><span style="color: #0000BB">sig_handler</span><span style="color: #007700">(</span><span style="color: #0000BB">$signo</span><span style="color: #007700">) <br />{<br /><br /> switch (</span><span style="color: #0000BB">$signo</span><span style="color: #007700">) {<br /> case </span><span style="color: #0000BB">SIGTERM</span><span style="color: #007700">:<br /> </span><span style="color: #FF8000">// handle shutdown tasks<br /> </span><span style="color: #007700">exit;<br /> break;<br /> case </span><span style="color: #0000BB">SIGHUP</span><span style="color: #007700">:<br /> </span><span style="color: #FF8000">// handle restart tasks<br /> </span><span style="color: #007700">break;<br /> default:<br /> </span><span style="color: #FF8000">// handle all other signals<br /> </span><span style="color: #007700">}<br /><br />}<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pcntl.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.pcntl.html">PCNTL Functions</a></div> <div class="up"><a href="book.pcntl.html">PCNTL</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?