<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<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
-->
