8.2 Generating Signals

Team-FLY

Every signal has a symbolic name starting with SIG . The signal names are defined in signal.h , which must be included by any C program that uses signals. The names of the signals represent small integers greater than 0. Table 8.1 describes the required POSIX signals and lists their default actions. Two signals, SIGUSR1 and SIGUSR2 , are available for users and do not have a preassigned use. Some signals such as SIGFPE or SIGSEGV are generated when certain errors occur; other signals are generated by specific calls such as alarm .

Table 8.1. The POSIX required signals.

signal

description

default action

SIGABRT

process abort

implementation dependent

SIGALRM

alarm clock

abnormal termination

SIGBUS

access undefined part of memory object

implementation dependent

SIGCHLD

child terminated , stopped or continued

ignore

SIGCONT

execution continued if stopped

continue

SIGFPE

error in arithmetic operation as in division by zero

implementation dependent

SIGHUP

hang-up (death) on controlling terminal (process)

abnormal termination

SIGILL

invalid hardware instruction

implementation dependent

SIGINT

interactive attention signal (usually Ctrl-C)

abnormal termination

SIGKILL

terminated (cannot be caught or ignored)

abnormal termination

SIGPIPE

write on a pipe with no readers

abnormal termination

SIGQUIT

interactive termination: core dump (usually Ctrl- )

implementation dependent

SIGSEGV

invalid memory reference

implementation dependent

SIGSTOP

execution stopped (cannot be caught or ignored)

stop

SIGTERM

termination

abnormal termination

SIGTSTP

terminal stop

stop

SIGTTIN

background process attempting to read

stop

SIGTTOU

background process attempting to write

stop

SIGURG

high bandwidth data available at a socket

ignore

SIGUSR1

user -defined signal 1

abnormal termination

SIGUSR2

user-defined signal 2

abnormal termination

Generate signals from the shell with the kill command. The name kill derives from the fact that, historically, many signals have the default action of terminating the process. The signal_name parameter is a symbolic name for the signal formed by omitting the leading SIG from the corresponding symbolic signal name.

  SYNOPSIS  kill -s signal_name pid...     kill -l [exit_status]     kill [-signal_name] pid...     kill [-signal_number] pid...  POSIX:Shell and Utilities  

The last two lines of the synopsis list the traditional forms of the kill command. Despite the fact that these two forms do not follow the POSIX guidelines for command-line arguments, they continue to be included in the POSIX standard because of their widespread use. The last form of kill supports only the signal_number values of for signal , 1 for signal SIGHUP , 2 for signal SIGINT , 3 for signal SIGQUIT , 6 for signal SIGABRT , 9 for signal SIGKILL , 14 for signal SIGALRM and 15 for signal SIGTERM .

Example 8.1

The following command is the traditional way to send signal number 9 ( SIGKILL ) to process 3423.

 kill -9 3423 
Example 8.2

The following command sends the SIGUSR1 signal to process 3423 .

 kill -s USR1 3423 
Example 8.3

The kill -l command gives a list of the available symbolic signal names. A system running Sun Solaris produced the following sample output.

 % kill -l HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM USR1 USR2 CLD PWR WINCH URG POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING LWP FREEZE THAW CANCEL LOST XRES RTMIN RTMIN+1 RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1 RTMAX 

Call the kill function in a program to send a signal to a process. The kill function takes a process ID and a signal number as parameters. If the pid parameter is greater than zero, kill sends the signal to the process with that ID. If pid is 0, kill sends the signal to members of the caller's process group . If the pid parameter is -1, kill sends the signal to all processes for which it has permission to send. If the pid parameter is another negative value, kill sends the signal to the process group with group ID equal to pid . Section 11.5 discusses process groups.

  SYNOPSIS  #include <signal.h>    int kill(pid_t pid, int sig);  POSIX:CX  

If successful, kill returns 0. If unsuccessful , kill returns “1 and sets errno . The following table lists the mandatory errors for kill .

errno

cause

EINVAL

sig is an invalid or unsupported signal

EPERM

caller does not have the appropriate privileges

ESRCH

no process or process group corresponds to pid

A user may send a signal only to processes that he or she owns. For most signals, kill determines permissions by comparing the user IDs of caller and target. SIGCONT is an exception. For SIGCONT , user IDs are not checked if kill is sent to a process that is in the same session. Section 11.5 discusses sessions. For security purposes, a system may exclude an unspecified set of processes from receiving the signal.

Example 8.4

The following code segment sends SIGUSR1 to process 3423 .

 if (kill(3423, SIGUSR1) == -1)    perror("Failed to send the SIGUSR1 signal"); 

Normally, programs do not hardcode specific process IDs such as 3423 in the kill function call. The usual way to find out relevant process IDs is with getpid , getppid , getgpid or by saving the return value from fork .

Example 8.5

This scenario sounds grim, but a child process can kill its parent by executing the following code segment.

 if (kill(getppid(), SIGTERM) == -1)     perror ("Failed to kill parent"); 

A process can send a signal to itself with the raise function. The raise function takes just one parameter, a signal number.

  SYNOPSIS  #include <signal.h>   int raise(int sig);  POSIX:CX  

If successful, raise returns 0. If unsuccessful, raise returns a nonzero error value and sets errno . The raise function sets errno to EINVAL if sig is invalid.

Example 8.6

The following statement causes a process to send the SIGUSR1 signal to itself.

 if (raise(SIGUSR1) != 0)    perror("Failed to raise SIGUSR1"); 

A key press causes a hardware interrupt that is handled by the device driver for the keyboard. This device driver and its associated modules may perform buffering and editing of the keyboard input. Two special characters , the INTR and QUIT characters, cause the device driver to send a signal to the foreground process group. A user can send the SIGINT signal to the foreground process group by entering the INTR character. This user-settable character is often Ctrl-C. The user-settable QUIT character sends the SIGQUIT signal.

Example 8.7

The stty -a command reports on the characteristics of the device associated with standard input, including the settings of the signal-generating characters. A system running Sun Solaris produced the following output.

 % stty -a speed 9600 baud; rows = 57; columns = 103; ypixels = 0; xpixels = 0; eucw 1:0:0:0, scrw 1:0:0:0 intr = ^c; quit = ^; erase = ^?; kill = ^u; eof = ^d; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^q; stop = ^s; susp = ^z; dsusp = ^y; rprnt = ^r; flush = ^o; werase = ^w; lnext = ^v; -parenb -parodd cs8 -cstopb hupcl cread -clocal -loblk -crtscts -parext -ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -iuclc ixon -ixany -ixoff imaxbel isig icanon -xcase echo echoe echok -echonl -noflsh -tostop echoctl -echoprt echoke -defecho -flusho -pendin iexten opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel 

The terminal in Example 8.7 interprets Ctrl-C as the INTR character. The QUIT character (Ctrl- above) generates SIGQUIT . The SUSP character (Ctrl-Z above) generates SIGSTOP , and the DSUSP character (Ctrl-Y above) generates SIGCONT .

The alarm function causes a SIGALRM signal to be sent to the calling process after a specified number of real seconds has elapsed. Requests to alarm are not stacked , so a call to alarm before the previous timer expires causes the alarm to be reset to the new value. Call alarm with a zero value for seconds to cancel a previous alarm request.

  SYNOPSIS  #include <unistd.h>   unsigned alarm(unsigned seconds);  POSIX  

The alarm function returns the number of seconds remaining on the alarm before the call reset the value, or 0 if no previous alarm was set. The alarm function never reports an error.

Example 8.8 simplealarm.c

Since the default action for SIGALRM is to terminate the process, the following program runs for approximately ten seconds of wall-clock time.

 #include <unistd.h> int main(void) {    alarm(10);    for ( ; ; ) ; } 
Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net