COHERENT manpages

This page displays the COHERENT manpage for signal() [Specify action to take upon receipt of a given signal].

List of available manpages
Index


signal() -- System Call (libc)

Specify action to take upon receipt of a given signal
#include <signal.h>
int (*signal(sigtype, function))()
int sigtype, (*function)();

A process  can receive a  signal, or interrupt, from  a hardware exception,
terminal  input, or  a kill()  call  made by  another process.   A hardware
exception  might be  caused  by an  illegal  instruction or  a bad  machine
address.   The terminal  interrupt character  (described  in detail  in the
Lexicon entry  tty) generates a process  interrupt (and in one  case a core
dump file for debugging purposes).

signal()  tells the  signal handler  what  to do  when the  current process
receives  signal sigtype.  sigtype  is the  signal to  process, as  defined
below.  function points to the routine to execute when sigtype is received.
This can  be a function  of your own  creation; or you  can use one  of the
following macros, which expand into pointers to system-defined functions:

SIG_DFL
     This  is the  default action.   The process terminates  just as  if it
     called the function exit(). In addition, the system writes a core file
     in the  current working directory if sigtype is  any of the following:
     SIGQUIT, SIGSYS, SIGTRAP, or SIGSEGV. (Note that this behavior applies
     only to executables for which  you have write permission.  If you lack
     write permission on an executable,  then no core file is written.) For
     more information on core files, see the Lexicon entry core.

SIG_IGN
     Ignore sigtype. The system discards all signals of this type.

signal() returns  a pointer to  the previous action.   If sigtype is  not a
recognized signal, signal() returns (int (*)())-1.

With the exception of SIGKILL and  SIGTRAP, caught signals are reset to the
default  action SIG_DFL.  To catch  a  signal again,  the routine  to which
function points must reissue the call to signal().

The following list names the  signals that signal() can process, as defined
in the  header file signal.h. Note  that the signal SIGKILL,  which kills a
process, can be neither caught  nor ignored.  Signals marked by an asterisk
produce a core dump if the action is SIG_DFL:

SIGHUP.........Hangup
SIGINT.........Interrupt
SIGQUIT*.......Quit
SIGILL*........Illegal instruction
SIGTRAP*.......Trace trap
SIGIOT.........IOT instruction
SIGABRT*.......To be replaced by SIGIOT
SIGEMT.........Emulator trap
SIGFPE*........Floating-point exception
SIGKILL........Kill
SIGBUS.........Bus error
SIGSEGV*.......Segmentation violation
SIGSYS*........Bad argument to system call
SIGPIPE........Write to pipe with no readers
SIGALRM........Alarm
SIGTERM........Software termination signal
SIGUSR1........User-defined signal
SIGUSR2........User-defined signal
SIGCLD.........Death of a child
SIGCHLD........Death of a child
SIGPWR.........Restart
SIGWINCH.......Window change
SIGPOLL........Polled event in stream

A signal may be caught during  a system call that has not yet returned.  In
this case,  the system call  appears to fail,  with errno set  to EINTR. If
desired, such  an interrupted  system call  may be reissued.   System calls
which may  be interrupted in this  way include pause(), read()  on a device
such as a terminal, write() on a pipe, and wait().

Example

The following program demonstrates signal(), kill(), getpid(), and fork().

#include <signal.h>

int got_it; /* Each side gets its own copy of all data at the fork */
int errset;

/*
 * Control comes here on SIGTRAP.  Do no I/O in signal function.
 * Reset the signal if you ever want another.
 */

void
sig_ser()
{
    got_it = 1; /* tell the child we got it */

    if (0 > signal(SIGTRAP, sig_ser))/* reset the signal */
        errset = 1;
}

main()
{
    int count;
    int child, parent;

    parent = getpid();  /* Both sides will get a copy */

    if (signal(SIGTRAP, sig_ser) < 0) {/* sets for both sides */
        perror("signal set failed");
        exit(0);
    }

    if (child = fork()) {   /* parent gets the child's id */
        for (count = 0; count < 3; count++) {
            kill(child, SIGTRAP);   /* signal the child */

            while(!got_it)      /* wait for signal */
                sleep(1);
            if (errset)
                perror("parent: signal reset failed");

            printf("parent got signal %d\n", count);
            got_it = errset = 0;
        }
        exit(0);
    }

    for (count = 0; count < 3; count++) {
        while(!got_it)          /* wait for signal */
            sleep(1);
        if (errset)
            perror("child: signal reset failed");
        printf("child got signal %d\n", count);/* show we got it */

        kill(parent, SIGTRAP);      /* signal the parent */
        got_it = errset = 0;
    }
    exit(0);
}

See Also

kill,
kill(),
libc,
ptrace(),
sh,
sigaction(),
signame,
sigset()
ANSI Standard, §7.7.1.1

Notes

The function signal() predates the sigset() and sigaction() sets of signal-
handling functions.   Never combine  signal() with  any of the  sigset() or
sigaction() families of functions: use one or the other, but not both.  For
a description  of how signal()  differs from sigset()  and sigaction(), see
their Lexicon entries.