A signal sends a message to a process and normally causes the process to terminate, usually due to some unexpected event, such as illegal division by zero, a segmentation violation, a bus error, or a power failure. The kernel also uses signals as timers, for example, to send an alarm signal to a process. The user sends signals when he hits the BREAK, DELETE, QUIT, or STOP keys.
The kernel recognizes 31 different signals, listed in /usr/include/signal.h. You can get a list of signals by simply typing kill -l at the UNIX prompt. (See Table 18.13.)
Name | Number | Default | Description |
---|---|---|---|
SIGHUP | 1 | Terminate | Hangup |
SIGINT | 2 | Interrupt | Interrupt |
SIGQUIT | 3 | Terminate | Quit/produces core file |
SIGILL | 4 | Terminate | Terminate |
[*] This is a partial listing of the signals.
Signals are asynchronous events; that is, the process doesn't know when a signal will arrive. Programmatically you can ignore certain signals coming into your process or set up a signal handler to execute a subroutine when the signal arrives. In Perl scripts, any signals you specifically want to handle are set in the %SIG associative array. If a signal is ignored, it will be ignored after fork or exec function calls.
A signal may be ignored or handled for a segment of your program and then reset to its default behavior. See Example 18.72.
Format$SIG{'signal'}; Example 18.72.
|
If you want to send a signal to a process or list of processes, the kill function is used. The first element of the list is the signal. The signal is a numeric value or a signal name if quoted. The function returns the number of processes that received the signal successfully. A process group is killed if the signal number is negative. You must own a process to kill it; that is, the effective uid and real uid must be the same for the process sending the kill signal and the process receiving the kill signal.
For complex signal handling, see the POSIX module in the Perl standard library.
Formatkill(LIST); kill LIST; Example 18.73.
|
The alarm function tells the kernel to send a SIGALARM signal to the calling process after some number of seconds. Only one alarm can be in effect at a time. If you call alarm and an alarm is already in effect, the previous value is overwritten.
The sleep function causes the process to pause for a number of seconds or forever if a number of seconds is not specified. It returns the number of seconds that the process slept. You can use the alarm function to interrupt the sleep.
Formatsleep(SECONDS); sleep SECONDS; sleep; Example 18.75.
|
For those using ActivePerl on Win32 systems, the following functions have not been implemented. Primary among these is alarm(), which is used in a few Perl modules. Because they're missing in ActivePerl, you can't use those modules. Here is a complete list of unimplemented functions:
Functions for processes and process groups:
alarm(), fork(), getpgrp(), getppid(), getpriority(), setpgrp(),
setpriority()Functions for fetching user and group info:
endgrent(), endpwent(), getgrent(), getgrgid(), getgrnam(),
getpwent(), getpwnam(), getpwuid(), setgrent(), setpwent()System V interprocess communication functions:
msgctl(), msgget(), msgrcv(), msgsnd(), semctl(), semget(),
semop(), shmctl(), shmget(), shmread(), shmwrite()Functions for filehandles, files, or directories:
link(), symlink(), chroot()
Input and output functions:
syscall()
Functions for fetching network info:
getnetbyname(), getnetbyaddr(), getnetent(), getprotoent(),
getservent(), sethostent(), setnetent(), setprotoent(),
setservent(), endhostent(), endnetent(), endprotoent(),
endservent(), socketpair()
See the perlport and perlwin32 documentation pages for more information on the portability of built-in functions in ActivePerl.