CHAPTER 16 - Parsing Command-Line Options - Signals

Computer Education, Training & Tutorial Resources - ComputerEducationWorld.com
Home » Free Computer Books » PHP » PHP 5 Power Programming »

In UNIX, signals are a basic mechanism to pass messages
between processes. They enable processes to tell each other that some type of
event has just occurred. This type of event is the only information passed to
basic UNIX signal handlers. There is another signal-handling mechanism
called “sigaction” in which signal handlers receive more information, but PHP
signals are based on the former, basic form. For example, if the user presses
Ctrl-c to stop a command-line program, the program receives an interrupt sig-
nal, called SIGINT.
In PHP, you can set up a function to handle one or more signals with the
pcntl_signal() function, like this:
<?php
function sigint_handler($signal) {
print “Interrupt!\n”;
exit;
}
pcntl_signal(SIGINT, “sigint_handler”);
declare (ticks = 1) {
while (sleep(1));
}
?>
This script sleeps until you terminate it. If you do press Ctrl-c, it prints
Interrupt! and exits. You could change this example to ignore Ctrl-c com-
pletely by changing the signal-handler function to the predefined SIG_IGN:
pcntl_signal(SIGINT, SIG_IGN);
You may change a signal handler anytime, including inside a signal-
handling function. To revert to the default signal handler, use SIG_DFL:
pcntl_signal(SIGINT, SIG_DFL);
PHP probably supports all the signals your system supports. Try typing
kill ­l in your shell to see some. Table 16.3 lists of signals that may be useful
from PHP, either catching and handling them, or sending them to (killing)
other processes.

Table 16.3
Signal

Description
SIGHUP
Hangup. Used to notify when terminal connection is lost.
SIGINT
Interrupt. Send when user hits the interrupt (Ctrl-c) key.
SIGABRT
Sent by the abort() C function; used by assert().
SIGKILL
Non-graceful termination of the process; cannot be caught.
SIGUSR1
User-defined signal 1.
SIGSEGV
Segmentation fault; in some operating systems, it’s known as General Protec-
tion Failure.
SIGUSR2
User-defined signal 2.
SIGPIPE
Sent when a pipe the process is reading closes unexpectedly.
SIGALRM
Sent when an alarm times out.
SIGTERM
Terminate process normally.
SIGCHLD
A child process just died or changed status.
SIGCONT
Continue after stopping with SIGSTOP.
SIGSTOP
Halt process; cannot be caught.
SIGTSTP
Halt process; may be caught.
SIGTTIN
Process stopped due to tty input.
SIGTTOU
Process stopped due to tty output.
SIGCXPU
CPU time limit exceeded.
SIGXFSZ
File size limit exceeded.
SIGBABY
Passed when a baby is ready to change diapers, hungry, about to climb some-
thing dangerous or doing anything else that requires immediate attention from
a parent PHP programmer.


• • •
 



captcha PHP Script Free PHP captcha script free php
Website Design by WebWalas.com