Docstoc

Signals

Document Sample
Signals Powered By Docstoc
					 I.    Relevant reading.
        A. Stevens Chapter 10; Chapter 15, Sections 15.1 and 15.2
         B. Cited man pages, in particular for sigaction and pages referenced therein.
         C. /usr/include/{signal.h,sys/signal.h,sys/iso/signal_iso.h}
II.    Introduction (Section 10.1).
        A. A signal is a program interrupt.
         B. Such an interrupt is an asynchronous event.
         C. Often, an interrupting signal occurs at a time not known in advance to the
              program being interrupted.
               1. Hence, the program cannot simply test some variable or call some other
                    function to know when a signal has occurred.
               2. Rather, the program has to tell the kernel "When this signal occurs, do
                    the following."
III.   Signal concepts (Section 10.2).
        A. In UNIX, all signals have names.
               1. The names begin with the prefix "SIG", followed by a reasonably
                    mnemonic suffix.
               2. E.g., SIGALRM is an alarm signal generated by a timer created with
                    the alarm or setitimer functions.
         B. Signals can be generated by a wide variety of sources, including:
               1. the user typing certain terminal-interrupt keys, such as ^C or ^Z, which
                    generate a SIGINT and SIGTSTP respectively 1
               2. hardware exceptions, such as divide by 0, invalid memory reference, and
                    the like
               3. the kill(2) function 2 that allows a process to send a signal to another
                    process
               4. the kill(1) shell command, which is an end-user interface to kill(2)
               5. software conditions, such as SIGALRM generated when a timer or alarm
                    goes off
 I.    There are three actions a process can take when a signal occurs.
        A. Ignore the signal.
               1. This works for most signals.
               2. Two that can never be ignored are SIGKILL and SIGSTOP.
               3. This allows the super user always to be able to terminate or stop any
                     process.
        B. Catch the signal.
               1. This is done by associating a function as the handler of a signal.
               2. The general way to do this is with the sigaction function, illustrated in
                     the example above.
               3. There is also a function named "signal" to set up a handler,
                     but sigaction supersedes it.
               4. Note that SIGKILL and SIGSTOP cannot be caught.
        C. Let the default action apply.
               1. All signals have a default action.
               2. For most it's to terminate the signaled process.
               3. Table 10.1 on Page 292 defines the default action for all signals.
II.    A compendium of UNIX signals.
        A. Different variants of UNIX define different signals.
        B. A good deal of them are common to all UNIX implementations.
        C. Table 10.1 on Page 292 lists all the POSIX signals, plus those defined in the four
              reference OS implementations covered in Stevens (i.e., FreeBSD, Linux, Mac OS,
              Solaris).
        D. Pages 293-298 provide a very nice summary of all the signals, including what
              they are used for.
III.   Details of the sigaction function (Section 10.14).
        A. Signature:
             int sigaction(int sig, const struct sigaction *restrict act,
                           struct sigaction *restrict oact);

        B.      Arguments:
                 1. The first argument sig is the signal whose action is being examined or
                     modified.
                 2. If the second act argument is non-null, the action is being modified.
                 3. If the third oact argument is non-null, the system returns the previous
                     action for the signal.
       C.      The struct definition is:
            struct sigaction {
                union {                                   /* handling function */
                    void (*sa_handler)(int);              /*
                    void (*sa_sigaction)(
                           int, siginfo_t*, void*);
                } funcptr;
                int sa_flags;                             /* options flags */
                sigset_t sa_mask;                         /* signals to block */
            }

IV.   Noteworthy signals (Section 10.2).
       A. SIGKILL -- kill a process; cannot be caught or ignored
       B. SIGSTOP -- stop a process; cannot be caught or ignored
       C. SIGSEGV -- invalid memory reference, leading to "Segmentation fault"
       D. SIGBUS -- implementation-defined hardware fault, leading to "Bus error"
       E. SIGTERM -- generated by kill(1), by default
       F. SIGINT -- sent by terminal driver in response to Control-C
       G. SIGTSTP -- sent by terminal driver in response to Control-Z
       H. SIGCHLD -- sent to parent when child terminates
        I. SIGALRM -- generated by calling alarm or setitimer
        J. SIGUSR1 and SIGUSR2 -- user-defined, for use in application programs
V.    The signal function and unreliable signals (Sections 10.3 and 10.4).
       A. signal is an older interface to signal features, superseded by sigaction.
       B. The signature is
       C.      typedef void Sigfunc(int);
       D.      Sigfunc* signal(int, Sigfunc*);
       E.      Using signal in its original form leads to unreliable signal handling, as discussed
               in Section 10.4.
       F.      The sigaction function, as standardized by POSIX, provides reliable signal
               handling.
       G.      Some UNIX systems implement signal using sigaction, making signal just a
               simpler interface to reliable signal processing (e.g., Mac OS X).
       H.      Other UNIX systems implement signal in its original unreliable form,
               presumably for backwards compatibility with older code (e.g., Solaris).
       I.      To avoid any possibility of unreliable signal handling, sigaction should always
               be used, either directly or in a re-implementation like the one in Section 10.14,
               Figure 10.18 (Page 328).
I.   A practical example of signal use (and starter kit for Programming Assignment 5).
      A. The following program illustrates the use of
           the setitimer and sigaction functions, in the generation and handling
           of SIGARLM signals.
             1. setitimer initiates an interval timer, that generates a SIGARLM as each
                   time interval elapses
             2. sigaction sets up the handler for SIGALRM
      B. Here is the simple-timer program:
   1    #include   <stdio.h>
   2    #include   <stdlib.h>
   3    #include   <unistd.h>
   4    #include   <termios.h>
   5    #include   <signal.h>
   6    #include   <sys/time.h>
   7
   8
   9    /****
  10     *
  11     * This program is a simple example of using setitimer and sigaction to
  12     * generate and handle SIGALRM signals. The program sets up an interval timer
  13     * that generates a SIGALRM once a second. The SIGALRM handler simply
  14     * increments a signal counter and prints out its value.
  15     *
  16     * Before starting the timer, the program puts the terminal in "raw mode".
  17     * This means that it does not echo input characters, wait for a newline to
  18     * complete an input, or respond to interrupt characters.
  19     *
  20     * After starting the timer and setting up the handler, the main function
waits
  21     * for the user to type the character 'q' to quit. Since the terminal is in
  22     * raw mode, typing 'q' is the only way to stop, since all other printable
  23     * characters are ignored, and terminal interrupt characters are disabled.
  24     *
  25     */
  26
  27
  28    /**
  29     * The ticks variable counts the number of one-second ticks since the program
  30     * started.
  31     */
  32    static int ticks;
  33
  34    /**
  35      * The tick function is the handler for SIGALRM.
  36      */
  37    void tick(int sig) {
  38         printf("%d\r", ++ticks);
  39    }
  40
  41    /**
  42     * Set the terminal to "raw" input mode. This is defined by setting terminal
  43     * control flags to noncanonical mode, turning off character echoing, and
  44     * ignoring signaling characters. Before setting to raw mode, save the
  45     * current mode so it can be restored later. After setting, return the
  46     * saved mode.
  47     *
  48     * For explanatory details, see Sections 18.10, 18.11 of Stevens, the
  49     * termio(7I) man page, and the tcsetattr(3C) man page. (To see a particular
  50     * man page section, use the "-s" argument, e.g., "man -s 7I termio" on
  51     * falcon/hornet.)
  52     */
  53    struct termios set_raw_term_mode() {
  54        struct termios cur_term_mode, raw_term_mode;
  55
  56        tcgetattr(STDIN_FILENO, &cur_term_mode);
  57        raw_term_mode = cur_term_mode;
  58        raw_term_mode.c_lflag &= ~(ICANON | ECHO | ISIG) ;
 59       raw_term_mode.c_cc[VMIN] = 1 ;
 60       raw_term_mode.c_cc[VTIME] = 0;
 61       tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_term_mode);
 62
 63       return cur_term_mode;
 64   }
 65
 66   /**
 67     * Restore the terminal mode to that saved by set_raw_term_mode.
 68     */
 69   void restore_term_mode(struct termios saved_term_mode) {
 70        tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_term_mode);
 71   }
 72
 73   /**
 74    * Set the terminal to raw mode, set up a one-second timer, set up the SIGARLM
 75    * handler, and then wait for the user to type 'q'.
 76    *
 77    * For details of timer setup, see Stevens Section 6.1 and the man pages for
 78    * setitimer(2) and gettimeofday(3C).
 79    *
 80    * For details of signal setup, see Stevens Section 10.14, and the man page
 81    * for sigaction.
 82    */
 83   int main(int argc, char** argv) {
 84
 85       struct termios saved_term_mode;       /* saved entering terminal mode */
 86       struct itimerval tbuf;                /* interval timer structure */
 87       struct sigaction action;              /* signal action structure */
 88
 89       /*
 90        * Explain to the user how to quit.
 91        */
 92       printf("Type 'q' to quit.\n\n");
 93
 94       /*
 95        * Initialize ticks to 0.
 96        */
 97       ticks = 0;
 98
 99       /*
100        * Set up the SIGALRM handler.
101        */
102       action.sa_handler = tick;      /* set tick to be the handler function */
103       sigemptyset(&action.sa_mask); /* clear out masked functions */
104       action.sa_flags   = 0;         /* no special handling */
105
106       /*
107         * Use the sigaction function to associate the signal action with SIGALRM.
108         */
109       if (sigaction(SIGALRM, &action, NULL) < 0 ) {
110            perror("SIGALRM");
111            exit(-1);
112       }
113
114       /*
115        * Define a one-second timer.
116        */
117       tbuf.it_interval.tv_sec = 1;
118       tbuf.it_interval.tv_usec = 0;
119       tbuf.it_value.tv_sec = 1;
120       tbuf.it_value.tv_usec = 0;
121
122       /*
123         * Use the setitimer function to start the timer.
124         */
125       if ( setitimer(ITIMER_REAL, &tbuf, NULL) == -1 ) {
126            perror("setitimer");
127            exit(-1);                   /* should only fail for serious reasons */
128       }
129
130       /*
131        * Set the terminal to raw mode.
132        */
133       saved_term_mode = set_raw_term_mode();
134
135       /*
136        * Busy wait until the user types 'q'.
137        */
138       while (getchar() != 'q') {}
139
140       /*
141        * Restore the terminal to the mode it was in at program entry.
142        */
143       restore_term_mode(saved_term_mode);
144
145   }

				
DOCUMENT INFO
Categories:
Tags:
Stats:
views:0
posted:3/20/2013
language:English
pages:7
qingqing19771029 qingqing19771029 http://
About