Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

pcntl_signal

Description

The pcntl_signal of PCNTL for PHP installs a signal handler.

Syntax

pcntl_signal(
    int $signal,
    callable|int $handler,
    bool $restart_syscalls = true
): bool

Parameters

signal

The signal number.

handler

The signal handler. This may be either of the two global constants: SIG_DFL or SIG_IGN, which will restore the default signal handler or ignore the signal, respectively; or a callable, which will be invoked to handle the signal.

If a callable is given, it must implement the following signature:

handler(
    int $signo,
    mixed $siginfo
): void
signo

The signal being handled.

siginfo

If operating systems supports siginfo_t structures, this will be an array of signal information dependent on the signal.

NOTE: Note that when you set a handler to an object method, that object's reference count is increased which makes it persist until you either change the handler to something else, or your script ends.

restart_syscalls

Specifies whether system call restarting should be used when this signal arrives.

Return

Returns true on success or false on failure.

Examples

1 · signal handler · SIG_DFL

<?

$signal = SIGHUP;
$handler = SIG_DFL;

$return = pcntl_signal($signal, $handler);

var_export($return);

?>
true

2 · signal handler · SIG_IGN

<?

$signal = SIGHUP;
$handler = SIG_IGN;

$return = pcntl_signal($signal, $handler);

var_export($return);

?>
true

3 · signal handler · callable

<?

function handler($signo, $siginfo)
{
    echo PHP_EOL . $signo . PHP_EOL;
    print_r($siginfo);
}

$signal = SIGHUP;
$handler = "handler";

$return = pcntl_signal($signal, $handler);

var_export($return);

$process_id = posix_getpid();

posix_kill($process_id, $signal);

pcntl_signal_dispatch();

?>
true
1
Array
(
    [signo] => 1
    [errno] => 0
    [code] => 0
)

4 · restart_syscalls · false

<?

function handler($signo, $siginfo)
{
    echo PHP_EOL . $signo . PHP_EOL;
    print_r($siginfo);
}

$signal = SIGHUP;
$handler = "handler";
$restart_syscalls = false;

$return = pcntl_signal($signal, $handler, $restart_syscalls);

var_export($return);

$process_id = posix_getpid();

posix_kill($process_id, $signal);

pcntl_signal_dispatch();

?>
true
1
Array
(
    [signo] => 1
    [errno] => 0
    [code] => 0
)

5 · restart_syscalls · true

<?

function handler($signo, $siginfo)
{
    echo PHP_EOL . $signo . PHP_EOL;
    print_r($siginfo);
}

$signal = SIGHUP;
$handler = "handler";
$restart_syscalls = true;

$return = pcntl_signal($signal, $handler, $restart_syscalls);

var_export($return);

$process_id = posix_getpid();

posix_kill($process_id, $signal);

pcntl_signal_dispatch();

?>
true
1
Array
(
    [signo] => 1
    [errno] => 0
    [code] => 0
)
HomeMenu