signal_action |
To control the reaction of a
current process to a particular signal or set of signals, use the action()
function. The action function takes either a signal or a set of signals as its
first argument and takes an os_signal_action object
as its second argument.
An os_signal_action
object specifies how a process reacts to signals. The default constructor
creates a signal action that reacts to the signal in the default way. Another
constructor creates a signal action that calls a used-supplied handler, which
takes the following parameters.
SA_RESETHAND
-Resets handler to the default action after the signal is received once.SA_INTERRUPT
-Interrupts automatic reset if a reentrant system call is executing when
the signal is received.SA_NOCLDSTOP
-Does not send a SIGCHLD to the current
process when one of its children terminates.void (*handler) (int) .
When the handler is called, the signal number is passed as its argument. If
the handler is equal to SIG_DFL , the default
action is performed. If the handler is equal to SIG_IGN
, the signal is ignored. For clarity, use restore_default()
and ignore() rather than the SIG_DFL
and SIG_IGN options. In the following example, the
current process installs a special handler for SIGALRM
and sets its alarm clock to generate a SIGALRM
signal after two seconds. The process then executes a pause()
, causing the process to sleep until a signal is received.
The first time the signal is
received, the handler executes and displays the received signal. Because the
handler is not installed with the SA_RESETHAND
option, the handler is restored to its default action.
The second time the signal is received, the default action is performed, displaying "Alarm Clock" and terminating without a core dump.
#include <iostream>
#include <ospace/unix.h>
void
alarm_handler( int signal )
{
cout << os_signal( signal ) << " was received" << endl;
}
int
main()
{
os_unix_toolkit initialize;
os_this_process::action( SIGALRM, alarm_handler );
cout << "Set my alarm for 2 seconds." << endl;
os_this_process::set_repeat_alarm( 2 );
cout << "Wait for a signal..." << endl;
os_this_process::pause();
cout << "Wait for a signal..." << endl;
os_this_process::pause();
return 0;
}
Set my alarm for 2 seconds.
Wait for a signal...
os_signal( 14, Alarm Clock ) was received
Wait for a signal...
os_signal( 14, Alarm Clock ) was received
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.