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.

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.

Example <ospace/unix/examples/signal7.cpp>
#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.