Format
#include <signal.h>
void ( *signal (int sig, void(*func)(int)) )(int);
Language Level: ANSI
Threadsafe: Yes.
Description
The signal() function allows a program to choose one of several ways to handle an interrupt signal from the operating system or from the raise() function. If compiled with the SYSIFCOPT(*ASYNCSIGNAL) option, this function uses asynchronous signals. The asynchronous version of this function behaves like sigaction() with SA_NODEFER and SA_RESETHAND options. Asynchronous signal handlers may not call abort() or exit(). The remainder of this function description will describe synchronous signals.
The sig argument must be one of the macros SIGABRT, SIGALL, SIGILL, SIGINT, SIGFPE, SIGIO, SIGOTHER, SIGSEGV, SIGTERM, SIGUSR1, or SIGUSR2, defined in the signal.h include file. SIGALL, SIGIO, and SIGOTHER are only supported by the ILE C/C++ runtime library. The func argument must be one of the macros SIG_DFL or SIG_IGN, defined in the <signal.h> include file, or a function address.
The meaning of the values of sig is as follows:
When SYSIFCOPT(*ASYNCSIGNAL) is specified, SIGALL is not a catch-all signal. A signal handler for SIGALL is only invoked for a user-raised SIGALL signal.
The action that is taken when the interrupt signal is received depends on the value of func.
Return Value
A return value of SIG_ERR indicates an error in the call to signal(). If successful, the call to signal() returns the most recent value of func. The value of errno may be set to EINVAL (the signal is not valid).
Example that uses signal()
This example shows you how to establish a signal handler.
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#define ONE_K 1024
#define OUT_OF_STORAGE (SIGUSR1)
/* The SIGNAL macro does a signal() checking the return code */
#define SIGNAL(SIG, StrCln) { \
if (signal((SIG), (StrCln)) == SIG_ERR) { \
perror("Could not signal user signal"); \
abort(); \
} \
}
void StrCln(int);
void DoWork(char **, int);
int main(int argc, char *argv[]) {
int size;
char *buffer;
SIGNAL(OUT_OF_STORAGE, StrCln);
if (argc != 2) {
printf("Syntax: %s size \n", argv[0]);
return(-1);
}
size = atoi(argv[1]);
DoWork(&buffer, size);
return(0);
}
void StrCln(int SIG_TYPE) {
printf("Failed trying to malloc storage\n");
SIGNAL(SIG_TYPE, SIG_DFL);
exit(0);
}
void DoWork(char **buffer, int size) {
int rc;
*buffer = malloc(size*ONE_K); /* get the size in number of K */
if (*buffer == NULL) {
if (raise(OUT_OF_STORAGE)) {
perror("Could not raise user signal");
abort();
}
}
return;
}
/* This is a program fragment and not a complete function example */
Related Information