ITEM: D8392L

How do I use signals in FORTRAN?




Question:

How do I use signals and implement a signal
handler using the signal system call without using the SIGNAL intrinsic
function in FORTRAN?

Response:

Discussed the use of the sample code sent to illuminate the use of
signal processing in FORTRAN.

Included here is the sample code.
HOW TO COMPILE THE EXAMPLE:
cc -g -c nuts.c
rm -f  sig_example 
xlf -o sig_example -qxflag=flttrap -qnofold -qlist sig_example.f nuts.o

FILE NUTS.C: 
/*
 * The signal include file is required if one wishes to use the symbol
 * (SIGXXX)
 * instead of a hard coded number for a given signal.
 */
\#include \
\#include \
\#include \
\#include \
\#include \

/*
 * Notice the declaration of the function (void) and the declaration 
 * of the
 * argument (void pointer to function).
 */ 
{  signal( SIGTRAP, ahandler);   /* and we never return  */
}
/*
 * And here's the correct way to couch the entry point for a handler
 */
void ahandler(int sig, int code, struct sigcontext *scp) 
{        fprintf( stderr, "An FP trap occurred\\n");
        exit( -1);
}
FILE SIG_EXAMPLE.F:
        program sig_example 
c**********************************************************************
        real*8 zero, one, big, small, res(8)
        data zero, one / 0.0d0, 1.0d0 /
        data big,small /1.7976931348623158d308,2.2250738585072015d-308/
        include '/usr/include/fpdc.h'
        include '/usr/include/fexcp.h' c
c**********************************************************************
        external ahandler               ! declare the handler extern
        external nuts                   ! ditto the  c function
c**********************************************************************
        call nuts( ahandler)            ! set SIGTRAP to handler in C 
c**********************************************************************
        res(1) = small * small          ! these won't trap because the
        res(2) = big * big              ! bits aren't enabled
        res(3) = one / zero
        res(4) = zero / zero
        write(6,100) (res(i),i=1,4)

        call fpgets(fpstat)             ! here we get the bits
        fpstat(fpze) = .true.           ! divide by zero
        fpstat(fpue) = .true.           ! underflow
        fpstat(fpoe) = .true.           ! overflow
        fpstat(fpve) = .true.           ! invalid operation
        call fpsets(fpstat)             ! here we set the bits

        res(1) = small * small          ! now the trap goes off
        res(2) = big * big
        res(3) = one / zero
        res(4) = zero / zero
        write(6,100) (res(i),i=1,4)

100     format('small * small result is :',e16.8,/,
     *  'big * big result is     :',e16.8,/,
     *  'one / zero result is    :',e16.8,/,
     *  'zero / zero result is   :',e16.8)
        end c
**********************************************************************
        block data                      ! this has to be here for SIGFPE
        include '/usr/include/fpdc.h'
        include '/usr/include/fpdt.h'
        end


Support Line: How do I use signals in FORTRAN? ITEM: D8392L
Dated: September 1993 Category: N/A
This HTML file was generated 99/06/24~13:30:55
Comments or suggestions? Contact us