SIGNAL(DOS) UNIX System V SIGNAL(DOS)
Name
signal - Defines the action to be taken upon receipt of a
signal.
Syntax
#include <signal.h>
void (*signal(sig, func))( )
int sig;
void (*func)( );
Description
The signal function allows a process to choose one of
several ways to handle an interrupt signal from the
operating system.
The sig argument must be one of the manifest constants
described below and defined in signal.h:
Signals and Responses
Value Modes Meaning Default Action
SIGABRT Real, Abnormal termination Terminates the
protected calling program
with exit code 3
SIGBREAK Protected CTRL+BREAK signal Terminates the
calling program
SIGFPE Real, Floating-point error Terminates the
protected calling program
SIGILL Real, Illegal instruction Terminates the
protected calling program
SIGINT Real, CTRL+C signal Terminates the
protected calling program
SIGSEGV Real, Illegal storage access Terminates the
protected calling program
SIGTERM Real, Termination request Terminates the
protected calling program
SIGUSR1 Protected OS/2 process flag A Signal is ignored
SIGUSR Protected OS/2 process flag B Signal is ignored
SIGUSR3 Protected OS/2 process flag C Signal is ignored
SIGUSR1, SIGUSR2, and SIGUSR3 are user-defined signals which
can be sent by means of DosFlagProcess. For details see the
Microsoft Operating System/2 Programmer's Reference.
The action taken when the interrupt signal is received
depends on the value of func. The func argument must be
either a function address or one of the manifest constants
defined in signal.h and listed below:
Value Meaning
SIG_IGN Ignores interrupt signal. This value
should never be given for SIGFPE,
since the floating-point state of the
process is left undefined.
SIG_DFL Uses system-default response. Under
DOS Versions 3.x or earlier, the
calling process is terminated and
control returns to the DOS command
level. If the calling program uses
stream I/O, buffers created by the
run-time library are not flushed (DOS
buffers are flushed).
Under OS/2, the system-default
response for all signals is to abort
the calling program, with the
exception of SIGUSR1, SIGUSR2, and
SIGUSR3, whose default is to ignore
the signal.
SIG_ERR Ignores interrupt signal (OS/2 only).
This constant is equivalent to SIG_IGN
except that any process that tries to
send this signal receives an error. A
process may use the raise function to
send a signal to itself. A different
process may send a signal by means of
the function DosFlagProcess (if the
signal is SIGUSR1, SIGUSR2, or
SIGUSR3) or by means of DosKillProcess
(if the signal is SIGTERM).
SIG_ACK Acknowledges receipt of a signal (OS/2
only). This constant is valid only 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
acknowledgment from the process. The
operating system does not queue up
signals of a given type; therefore, if
more than one signal of a given type
accumulates before the process returns
a SIG_ACK value, only the most recent
signal is sent to the process after
the SIG_ACK value is received by the
operating system. This option has no
effect on which handler is installed
for a given signal. The manifest
constant SIG_ACK is not supported for
SIGFPE signals.
Function Installs the specified function as the
address handler for the given signal.
For SIGFPE signals, the function is
passed two arguments: SIGFPE and the
floating-point error code identifying
the type of exception that occurred.
For SIGINT signals under DOS Versions
3.x or earlier, the function is passed
the single argument SIGINT and
executed.
Under OS/2, for all signals other than
SIGFPE, the function is passed two
arguments: the signal number and the
argument furnished by the
DosFlagProcess function, if the signal
is SIGUSR1, SIGUSR2, or SIGUSR3.
For SIGFPE, the function pointed to by func is passed two
arguments, SIGFPE and an integer error subcode, FPE_xxx;
then the function is executed. (See the include file float.h
for definitions of the FPE_xxx subcodes.) The value of func
is not reset upon receiving the signal. To recover from
floating-point exceptions, use setjmp in conjunction with
longjmp. (See the example under _fpreset.) If the function
returns, the calling process resumes execution with the
floating-point state of the process left undefined.
If the function returns, the calling process resumes
execution immediately following the point at which it
received the interrupt signal. This is true regardless of
the type of signal or operating mode.
Before the specified function is executed under DOS Versions
3.x or earlier, the value of func is set to SIG_DFL. The
next interrupt signal is treated as described above for
SIG_DFL, unless an intervening call to signal specifies
otherwise. This allows the user to reset signals in the
called function if desired.
Under OS/2, the signal handler is 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.
Since signal-handler routines are normally called
asynchronously when an interrupt occurs, it is possible that
the your signal-handler function will get control when a C
run-time operation is incomplete and in an unknown state.
Certain restrictions apply to the C functions that can be
used in your signal-handler routine, as summarized below:
Do not issue low-level or stdio.h I/O routines (for
example, printf and fread).
Do not call heap routines or any routine that uses the
heap routines (for example, malloc, freect, strdup,
putenv).
Do not use any C function that generates a system call
(for example, getcwd, time).
Do not use the longjmp function.
Do not use any overlay routines.
Return Value
The signal function returns the previous value of func
associated with the given signal. For example, if the
previous value of func was SIG_IGN, the return value will be
SIG_IGN. The one exception to this rule is SIG_ACK, which
returns the address of the currently installed handler.
A return value of -1 indicates an error, and errno is set to
EINVAL. The possible error causes are an invalid sig value,
an invalid func value (that is, a value that is less than
SIG_ACK but not defined), or a func value of SIG_ACK used
when no handler is currently installed.
See Also
abort(DOS), exit(DOS), _exit(DOS), _fpreset(DOS), spawn(DOS)
Notes
Signal settings are not preserved in child processes created
by calls to exec or spawn. The signal settings are reset to
the default in the child process.
Example
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <process.h>
int handler();
main()
{
/* will cause the CTRL+C interrupt to call "handler": */
if (signal(SIGINT,handler) == (int(*)())-1)
{
fprintf(stderr,"Couldn't set SIGINT");
abort();
}
for(;;)printf("Hit control C:\n");
}
int handler() /* Function called at system interrupt */
{
char ch;
signal(SIGINT, SIG_IGN); /* Disallow CTRL+C */
/* during handler */
printf("Terminate processing? ");
ch = getch();
if (( ch == 'y' ) || ( ch == 'Y')) exit(0);
signal(SIGINT,handler);/* This is necessary so that the
** next CTRL+C interrupt will call
** "handler", since the DOS 3.x
** operating system resets the
** interrupt handler to the
** system default after the
** user-defined handler is called */
}
This program uses the signal function to set up the handler
function as the routine that is called to execute an
operating-system interrupt. When the user presses CTRL+C,
handler is called to handle the interrupt.
(printed 7/6/89)