Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>signal() set interrupt signal handling</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 signal()                Set Interrupt Signal Handling

 #include   <signal.h>

 void       (*signal(sig,func()))();
 int        sig;                         Signal value
 void       *func();                     Pointer to function to be executed

    signal() redirects the operating system interrupt signal 'sig' to the
    function 'func()'.  This allows a process to change the way in which
    the interrupt is handled.  'sig' must be one of the following signals
    (defined in <signal.h>):

              MS-
    Value     DOS  OS/2 Meaning                Default Action
    --------  ---  ---  ---------------------  --------------------------
    SIGABRT   Yes  Yes  Abnormal termination   Terminates calling program
                                               with exit code 3
    SIGBREAK  No   Yes  CTRL+BREAK signal      Terminates calling program

    SIGFPE    Yes  Yes  Floating point error   Terminates calling program

    SIGILL  * Yes  Yes  Illegal Instruction    Terminates calling program

    SIGINT    Yes  Yes  CTRL+C signal          Terminates calling program

    SIGSEGV * Yes  Yes  Illegal storage access Terminates calling program

    SIGTERM * Yes  Yes  Termination request    Terminates calling program

    SIGUSR1   No   Yes  OS/2 process flag A    Signal is ignored

    SIGUSR2   No   Yes  OS/2 process flag B    Signal is ignored

    SIGUSR3   No   Yes  OS/2 process flag C    Signal is ignored

    SIGUSR1, SIGUSR2, and SIGUSR3 are user-defined signals that can be
    sent by means of DOSFLAGPROCESS() in OS/2.

    * Per MSC: These signals are not generated by MS-DOS but are
    supported for ANSI compatibility.

    When the interrupt signal is received, the resulting action is
    determined by the value of 'func'. 'func' must be a function address
    or one of the following manifest constants (defined in <signal.h>):

    Value       Meaning
    ----------  --------------------------------------------------
    SIG_IGN:    Ignore interrupt signal.  Do not use this value with
                SIGFPE, or the state of the floating point processor will
                be left undefined after an exception.

    SIG_DFL:    Uses system-default response.

                Under MS-DOS the calling process is terminated and
                control is returned to MS-DOS command level. Although
                this closes all files opened by the process, it does not
                flush stream I/O buffers (but MS-DOS buffers are
                flushed).

                Under OS/2, the system-default response for all signals
                is to abort the calling program (except for SIGUSR1,
                SIGUSR2, and SIGUSR3, whose default is to ignore the
                signal).

    SIG_ERR:    Ignores interrupt signal (OS/2 only).

                SIG_ERR is equivalent to SIG_IGN except that any process
                that tries to send this signal receives an error. A
                process may use raise() to signal itself. A different
                process may send a signal using DOSFLAGPROCESS() (if the
                signal is SIGUSR1, SIGUSR2, or SIGUSR3), or using
                DOSKILLPROCESS() (if the signal is SIGTERM).

    SIG_ACK:    Acknowledges receipt of signal (OS/2 only).

                SIG_ACK is only valid if a user-defined signal handler is
                installed. Once a process receives a given signal, the
                operating system does not send any more signals of this
                type until it receives a SIG_ACK acknowledgement back
                from the process.

                OS/2 does not queue up signals of a given type, so if
                more than one signal of a given type accumulates before
                the process sends back a SIG_ACK value, only the most
                recent signal is sent to the process after SIG_ACK is
                received by the operating system.

                This option has no effect on which handler is installed
                for a given signal. The manifest constant is not
                supported for SIGFPE signals.

 Function address:  Installs the specified function as the handler for
                    the given signal.

                For SIGFPE signals, the function pointed to by 'func' is
                passed the two arguments SIGFPE and an integer error
                subcode, FPE_xxx, indicating one of the following
                floating-point exceptions:

                    FPE_INVALID           Invalid operation
                    FPE_DENORMAL          Denormal value
                    FPE_ZERODIVIDE        Divide by zero
                    FPE_OVERFLOW          Overflow
                    FPE_UNDERFLOW         Underflow
                    FPE_UNEMULATED        Unemulated instruction
                    FPE_SQRTNEG           Square root of negative number
                    FPE_STACKOVERFLOW     FP stack overflow
                    FPE_STACKUNDERFLOW    FP stack underflow

                    To recover from floating point exceptions, use
                    setjmp() together with longjmp(). If the function
                    returns, the calling process resumes execution with
                    the floating-point state of the process left
                    undefined.


                For SIGINT signals under MS-DOS the function pointed to
                by 'func' is passed the single argument SIGINT and
                executed.

                Under OS/2, for all signals other than SIGFPE, the
                installed function is passed two arguments: the signal
                number and the argument furnished by DOSFLAGPROCESS(), if
                the signal is SIGUSR1, SIGUSR2, or SIGUSR3.

                If the function returns, the calling process resumes
                execution immediately following the point at which it
                received the interrupt signal.

                Under MS-DOS, before the function is executed the value
                of 'func' is set to SIG_DFL. The next interrupt signal is
                then handled as described by SIG_DFL, above, unless
                signal() is called again. This allows the user to reset
                signals in the called function if desired.

                Under OS/2, the signal handler in not reset to the
                system-default response. Instead, no signals of a given
                type are received by a process until the process sends a
                SIG_ACK value to the operating system. The user can
                restore the system-default response from the handler by
                first sending SIG_DFL and then sending SIG_ACK to the
                operating system.

    Returns:    If successful, the value of the previous interrupt
                handler is returned (except in the case of SIG_ACK, which
                returns the address of the currently installed handler).

                If unsuccessful, SIG_ERR is returned and 'errno' (defined
                in <stdlib.h>) is set to EINVAL (defined in <errno.h>).
                Possible error causes are an invalid 'sig' value, an
                invalid 'func' value (that is, a value less than SIG_ACK
                but not defined), or a 'func' value of SIG_ACK used when
                no handler is currently installed.

      Notes:    Signal settings are reset to the default in child
                processes created by calls to any of the exec...() or
                spawn...() functions.

   -------------------------------- Example ---------------------------------

    The following statements redirect the control-C handler to the
    function ctrl_c_handler() and redirect floating-point exceptions to
    the function fp_err_handler().

           #include <stdlib.h>           /*for exit, abort and perror */
           #include <stdio.h>            /*for printf and scanf*/
           #include <signal.h>           /*for signal, SIGINT and SIGFPE*/
           #include <float.h>            /*for _fpreset*/
           #include <setjmp.h>           /*for setjmp, longjmp and jmp_buf*/

           int ctrl_c_handler(), fp_err_handler();
           jmp_buf jmp_mark;

           main()
           {
               if (signal(SIGINT,ctrl_c_handler) == (int(*)())-1) {
                   perror("couldn't set SIGINT");
                   abort();
               }
               if (signal(SIGFPE,fp_err_handler) == (int(*)())-1) {
                  perror("couldn't set SIGFPE");
                  abort();
               }
               if (setjmp(jmp_mark)!=0) {     /* true on FP error */
                   printf("recovered from floating point error\n");
                   exit(0);
               }
           }

           int ctrl_c_handler(sig)
           int sig;
           {
               char c;

               printf("Really quit?");
               scanf("%1c",&c);
               if (c == 'y' || c == 'Y')
                   exit(0);
               signal(SIGINT,ctrl_c_handler);
           }

           int fp_err_handler(sig,subcode)
           int sig,subcode;
           {
               printf("floating point error #%d\n",subcode);
               _fpreset();             /*reinit FP package */
               longjmp(jmp_mark,-1);
           }


See Also: exit() _exit() _fpreset() longjmp() setjmp()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson