# SIGNAL

Portability Function: Controls interrupt signal handling and changes the action for a specified signal.

## Module

USE IFPORT

result = SIGNAL (signum, proc, flag)

signum | (Input) INTEGER(4). Number of the signal to change. The numbers and symbolic names are listed in a table below.
proc | (Input) Name of a signal-handler routine. It must be declared EXTERNAL and INTEGER(4). This routine is called only if flag is negative.
flag | (Input) INTEGER(4). If negative, the user's proc routine is called. If 0, the signal retains its default action; if 1, the signal should be ignored.

## Results

The result type is INTEGER(4) on IA-32 architecture; INTEGER(8) on Intel® 64 architecture. The result is the previous value of proc associated with the specified signal. For example, if the previous value of proc was SIG_IGN, the return value is also SIG_IGN. You can use this return value in subsequent calls to SIGNAL if the signal number supplied is invalid, if the flag value is greater than 1, or to restore a previous action definition.

A return value of SIG_ERR indicates an error, in which case a call to IERRNO returns EINVAL. If the signal number supplied is invalid, or if the flag value is greater than 1, SIGNAL returns -(EINVAL) and a call to IERRNO returns EINVAL.

An initial signal handler is in place at startup for SIGFPE (signal 8); its address is returned the first time SIGNAL is called for SIGFPE. No other signals have initial signal handlers.

Be careful when you use SIGNALQQ or the C signal function to set a handler, and then use the Portability SIGNAL function to retrieve its value. If SIGNAL returns an address that was not previously set by a call to SIGNAL, you cannot use that address with either SIGNALQQ or C's signal function, nor can you call it directly. You can, however, use the return value from SIGNAL in a subsequent call to SIGNAL. This allows you to restore a signal handler, no matter how the original signal handler was set.

The signal-handler argument proc accepts a single INTEGER(4) argument, which is the number of the signal to be handled. The function must use the compiler's default calling conventions. If you have used compiler options, such as option iface, to change the default conventions, add the following directive to the signal handler function to reset the calling conventions:

!DIR$ ATTRIBUTES DEFAULT :: the-routine-specified-in-proc

The signal-handler function returns an INTEGER(4) value. If the function has handled the signal, the value returned is an integer, but the value is not used.

Because signal-handler routines are usually called asynchronously when an interrupt occurs, it is possible that your signal-handler function will get control when a run-time operation is incomplete and in an unknown state. You cannot use the following kinds of signal-handler routines:

- Routines that perform low-level (such as FGETC) or high-level (such as READ) I/O.

- Heap routines or any routine that uses the heap routines (such as MALLOC and ALLOCATE).

- Functions that generate a system call (such as TIME).

The following table lists signals, their names and values:

Symbolic name | Number | Description
--- | --- | ---
SIGABRT | 6 | Abnormal termination
SIGFPE | 8 | Floating-point error
SIGKILL1 | 9 | Kill process
SIGILL | 4 | Illegal instruction
SIGINT | 2 | CTRL+C signal
SIGSEGV | 11 | Illegal storage access
SIGTERM | 15 | Termination request
1SIGKILL can be neither caught nor ignored.

The default action for all signals is to terminate the program with exit code.

ABORT does not assert the SIGABRT signal. The only way to assert SIGABRT or SIGTERM is to use KILL.

SIGNAL can be used to catch SIGFPE exceptions, but it cannot be used to access the error code that caused the SIGFPE. To do this, use SIGNALQQ instead.

## Example

        USE IFPORT
        EXTERNAL h_abort
        INTEGER(4) :: h_abort
        INTEGER(4) iret1, iret2, procnum
        iret1 = SIGNAL(SIGABRT, h_abort, -1)
        WRITE(*,*) 'Set signal handler #1. Return = ', iret1

        procnum = getpid( )
        iret2 = KILL(procnum, SIGABRT)
        WRITE(*,*) 'Raised signal. Return = ', iret2
        END
     !
     !  Signal-handler routine
     !
        INTEGER(4) FUNCTION h_abort (sig_num)
        INTEGER(4) sig_num
        !DIR$ ATTRIBUTES DEFAULT :: h_abort
        WRITE(*,*) 'In signal handler function h_abort for SIG$ABORT'
        WRITE(*,*) 'signum = ', sig_num
        h_abort = 1
        END

Parent topic: S

## See Also

SIGNALQQ