| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 
 | <?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 331331 $ -->
<chapter  xml:id="event.constructing.signal.events" xmlns:phpdoc="http://php.net/ns/phpdoc" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <title>Constructing signal events</title>
 <para>
  Event can also watch for POSIX-style signals. To construct a handler for a
  signal, use
  <methodname>Event::__construct</methodname>
  constructor with
  <constant>Event::SIGNAL</constant>
  flag, or
  <methodname>Event::signal</methodname>
  factory method.
 </para>
 <example>
  <title>Handling <literal>SIGTERM</literal> signal</title>
  <programlisting role="php">
<![CDATA[
<?php
/*
Launch it in a terminal window:
$ php examples/signal.php
In another terminal window find out the pid and send SIGTERM, e.g.:
$ ps aux | grep examp
ruslan    3976  0.2  0.0 139896 11256 pts/1    S+   10:25   0:00 php examples/signal.php
ruslan    3978  0.0  0.0   9572   864 pts/2    S+   10:26   0:00 grep --color=auto examp
$ kill -TERM 3976
At the first terminal window you should catch the following:
Caught signal 15
*/
class MyEventSignal {
    private $base, $ev;
    public function __construct($base) {
        $this->base = $base;
        $this->ev = Event::signal($base, SIGTERM, array($this, 'eventSighandler'));
        $this->ev->add();
    }
    public function eventSighandler($no, $c) {
        echo "Caught signal $no\n";
        $this->base->exit();
    }
}
$base = new EventBase();
$c    = new MyEventSignal($base);
$base->loop();
?>
]]>
  </programlisting>
 </example>
 <para>
  Note that signal callbacks are run in the event loop after the signal
  occurs, so it is safe for them to call functions that you are not supposed
  to call from a regular POSIX signal handler.
 </para>
 <para></para>
 <para>
  See also
  <link
 xlink:href="http://www.wangafu.net/~nickm/libevent-book/Ref4_event.html#_constructing_signal_events">Fast
 portable non-blocking network programming with Libevent, Constructing Signal
 Events</link>
  .
 </para>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
 |