[Previous] [Contents] [Next]

raise()

Generate a signal

Synopsis:

#include <signal.h>

int raise( int condition );

Library:

libc

Description:

The raise() function generates the signal specified by condition. The possible signals are defined in <signal.h>, and are documented with the signal() function.

Use the SignalAction() or signal() functions to specify the actions to take when a signal is received.

Returns:

0 if the specified condition is sent, or nonzero if an error occurs (errno is set).

The raise() function doesn't return if the action for that signal is to terminate the program or to transfer control using the longjmp() function.

Errors:

EAGAIN
Insufficient system resources are available to deliver the signal.
EINVAL
The value of condition isn't a valid signal number.

Examples:

/*
 * This program waits until a SIGINT signal is received.
 */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

sig_atomic_t signal_count;
sig_atomic_t signal_number;

void alarm_handler( int signum )
{
    ++signal_count;
    signal_number = signum;
}

int main( void )
{
    unsigned long i;

    signal_count = 0;
    signal_number = 0;
    signal( SIGINT, alarm_handler );

    printf( "Signal will be auto-raised on iteration "
            "10000 or press Ctrl-C.\n" );
    printf("Iteration:      ");
    for( i = 0; i < 100000; ++i ) {
        printf( "\b\b\b\b\b%*d", 5, i );

        if( i == 10000 ) raise( SIGINT );

        if( signal_count > 0 ) break;
    }

    if( i == 100000 ) {
        printf( "\nNo signal was raised.\n" );
    } else if( i == 10000 ) {
        printf( "\nSignal %d was raised by the "
                "raise() function.\n", signal_number );
    } else {
        printf( "\nUser raised signal #%d.\n",
                signal_number );
    }

    return EXIT_SUCCESS;
}

Classification:

ANSI

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

signal(), SignalAction()


[Previous] [Contents] [Next]