Using the signal classes

Signals allow one processes to interrupt the execution of another process.

Signal handlers allow processes to intercept and react to the signals sent to them.

Rudiments provides 3 classes for working with signals: signalset, signalmanager and signalhandler.

A signal set is just a collection of signals. The signalset class allows a programmer to build up a collection of signals.

The signalmanager class provides methods for sending signals, ignoring signals, waiting for signals and examining blocked signals.

The signalhandler class provides methods for catching and handling signals.

This program illustrates the use of all three classes.

// Copyright (c) 2001  David Muse
// See the file COPYING for more information

#include <rudiments/signalclasses.h>
#include <rudiments/stdio.h>

bool    gotsigterm=false;

void handleSigterm(int32_t sig) {
        stdoutput.printf("Got a SIGTERM!\n");

#ifdef SIGALRM
void handleAlarm(int32_t sig) {

int main(int argc, const char **argv) {

        // ignore all signals except SIGTERM and SIGALRM
        signalset       ignoreset;
        #ifdef SIGALRM

        // run handleSigterm() when a SIGTERM is received
        signalhandler   termhandler;

        // run handleAlarm() when a SIGALRM is received
        #ifdef SIGALRM
        signalhandler   alarmhandler;

        // Loop forever, waiting to receive a signal that we are not ignoring.
        while (!gotsigterm) {

It should print out "alarm!" every 2 seconds and, if killed, should print out "Got a SIGTERM!" before exiting. If killed with a signal other than SIGTERM (the default), then it will ignore the signal.

Windows systems don't have signals or the kill program. Signals are more-or-less simulated on Windows platforms though, and the following program provides an approximation of the kill program as found on unix-like systems, that should work on Windows.

#include <rudiments/process.h>
#include <rudiments/charstring.h>
#include <rudiments/error.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

        if (argc<3) {
                stdoutput.printf("usage: kill signal processid\n");

        int32_t sig=SIGTERM;
        if (!charstring::compare(argv[1],"SIGINT")) {
        } else if (!charstring::compare(argv[1],"SIGABRT")) {
        } else if (!charstring::compare(argv[1],"SIGFPE")) {
        } else if (!charstring::compare(argv[1],"SIGILL")) {
        } else if (!charstring::compare(argv[1],"SIGSEGV")) {
        } else if (!charstring::compare(argv[1],"SIGKILL")) {

        pid_t   pid=charstring::toInteger(argv[2]);

        if (!process::sendSignal(pid,sig)) {
                stdoutput.printf("kill failed: %s\n",error::getErrorString());