[Previous]
[Contents]
[Next]

signal()

set handling for exceptional conditions

Synopsis:

#include <signal.h>
void ( *signal(int sig, 
               void (*func)(int)) )( int );

Description:

The signal() function is used to specify an action to take place when certain conditions are detected while a program executes. See the <signal.h> header file for definitions of these conditions, and also refer to the QNX System Architecture manual.

There are three types of actions that can be associated with a signal: SIG_DFL, SIG_IGN or a pointer to a function. Initially, all signals are set to SIG_DFL or SIG_IGN prior to entry of the main() routine. An action can be specified for each of the conditions, depending upon the value of the func argument, as discussed below.


Note: The SIGKILL and SIGSTOP signals can be caught or ignored only if _PPF_IMMORTAL is set in the system flags word of the process; see qnx_pflags() for more information.

func is a function

When func is a function name, that function is called in a manner equivalent to the following code sequence:

    /* "sig_no" is condition being signalled */
    signal( sig_no, SIG_DFL );
    (*func)( sig_no );

The func function may do the following:

If you use longjmp() to return from a signal handler, the signal remains masked. You can use siglongjmp() to restore the mask to the state saved in a previous call to sigsetjmp().

After returning from the signal-catching function, the receiving process resumes execution at the point at which it was interrupted.

The signal catching function is described as follows:

    void func( int sig_no )
    {

      /* body of function */

    }

Note: It isn't possible to catch the SIGSTOP or SIGKILL signals.

Since signal-catching functions are invoked asynchronously with process execution, the type sig_atomic_t may be used to define variables on which an atomic operation (for example, incrementation or decrementation) may be performed.

func is SIG_DFL

If func is SIG_DFL, the default action for the condition is taken.

If the default action is to stop the process, the execution of that process is temporarily suspended. When a process stops, a SIGCHLD signal is generated for its parent process, unless the parent process has set the SA_NOCLDSTOP flag (see the sigaction() function). While a process is stopped, any additional signals that are sent to the process aren't delivered until the process is continued, except SIGKILL, which always terminates the receiving process.

Setting a signal action to SIG_DFL for a signal that's pending, and whose default action is to ignore the signal (for example, SIGCHLD), causes the pending signal to be discarded, whether or not it's blocked.

func is SIG_IGN

If func is SIG_IGN, the indicated condition is ignored.


Note: The action for the SIGSTOP and SIGKILL signals can't be set to SIG_IGN.

Setting a signal action to SIG_IGN for a signal that's pending causes the pending signal to be discarded, whether or not it's blocked.

(QNX extension) If a process sets the action for the SIGCHLD signal to SIG_IGN:

Handling a condition

When a condition is detected, it may be handled by a program, it may be ignored, or it may be handled by the usual default action (often causing an error message to be printed on the stderr stream followed by program termination).

A condition can be generated by a program using the raise() or kill() function.

Returns:

A return value of SIG_ERR indicates that the request couldn't be handled, and errno is set to the value EINVAL.

Otherwise, the previous value of func for the indicated condition is returned.

Examples:

#include <signal.h>

sig_atomic_t signal_count;

void MyHandler( int sig_number )
  {
     ++signal_count;
  }

void main()
  {
    signal( SIGFPE, MyHandler );   /* Set own handler */
    signal( SIGABRT, SIG_DFL );    /* Default action */
    signal( SIGFPE, SIG_IGN );     /* Ignore condition */
  }

Classification:

ANSI

The behavior when the action for the SIGCHLD signal is set to SIG_IGN is a QNX extension.

Safety:
Interrupt handler No
Signal handler Yes, but modifies errno
Thread Yes

See also:

errno, kill(), qnx_pflags(), raise(), sigaction(), sigprocmask()


[Previous]
[Contents]
[Next]