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 - <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>

 int        (*signal(sig,func))();
 int        sig;                         Signal value
 int        (*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
    (defined in <signal.h>) signals:

            SIGINT     Redirect MS-DOS interrupt INT 23H (control-C)
            SIGFPE     Redirect unmasked floating-point exceptions

    'func' must be either one of the manifest constants (defined in
    <signal.h>) SIG_IGN, or SIG_DFL, or a function address.  The action
    taken when the interrupt signal is received is determined by the
    value of 'func', as follows:

    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:    Terminate calling process and return control to MS-DOS
                command level.  All files opened by the process are
                closed, but buffers are not flushed.

Function address, with SIGINT
                The function pointed to by 'func' is passed the single
                argument SIGINT. Before 'func' is executed, the SIGINT
                interrupt is redirected to SIG_DFL; this causes the next
                SIGINT interrupt to be handled by SIG_DFL, unless an
                intervening call to signal() redirects it elsewhere.  The
                function 'func' can redirect SIGINT to itself in order to
                continue handling the interrupt signal.  If 'func()'
                returns, the execution continues where the interrupt
                signal was received.

Function address, with SIGFPE
                The function pointed to by 'func' is passed the two
                arguments SIGFPE and an integer error subcode, FPE_xxx.
                FPE_xxx is 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

    Returns:    If successful, the value of the previous interrupt
                handler is returned. -1 is returned on an error, and
                'errno' (defined in <stdlib.h>) is set to EINVAL (defined
                in <errno.h>) to indicate an invalid 'sig' value.

      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