Home > Articles, Operating Systems > Implementation of functions in C for software signal management in POSIX operating systems – Version 1.

Implementation of functions in C for software signal management in POSIX operating systems – Version 1.

Below I quote a simple implementation in C that you can use in your applications for the management of software signals in POSIX operating systems (such as the GNU/Linux). Of course, this implementation comes from a personal project of the past, especially where I grappled with system programming. You can modify this implementation to work best with your needs.

Source code of the implementation:

/*
 *  signals_handling.c -- signals handling support.
 *
 *  Copyright (C) 2007  Efstathios Chatzikyriakidis (contact@efxa.org)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/* standard libraries' includes. */
#include <stdlib.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

/* function prototypes. */
static void term_handler (const int sig);
static void chld_handler (const int sig);
static void signals_handler (const int sig);
void signals_support (const int state);

/* the following function cleans up all the zombie
   processes each time is called. is a handler for
   the `SIGCHLD' signal. */
static void
chld_handler (const int sig) {
  /* are used for cleaning up zombies. */
  int status;
  pid_t pid;

  /* clean up all zombie processes. */
  while ((pid = waitpid (-1, &status, WNOHANG)) > 0);

  /* status variables may be used in future. */
}

/* the following function is `SIGTERM' signal's handler. */
static void
term_handler (const int sig) {
  /* log informative message. */
  syslog (LOG_INFO, "termination signal found.");

  /* terminate the program. */
  exit (EXIT_FAILURE);
}

/* the following function is called when a new
   fresh signal occures. then calls a specific
   handler depending for that signal. */
static void
signals_handler (const int sig) {
  switch (sig) {
    case SIGTERM:
      term_handler (sig);
      break;

    case SIGCHLD:
      chld_handler (sig);
      break;

    default: /* for all the other signals. */
      syslog (LOG_INFO, "signal found: `%d'.", sig);
      break;
  }
}

/* the following function enables or
   disables signals handling support. */
void
signals_support (const int state) {
  /* is used for handling the signals. */
  register int i;

  /* enable signals handler. */
  if (state)
    for (i = SIGHUP; i <= SIGTERM; i++)
      signal (i, signals_handler);

  /* disable signals handler. */
  if (!state)
    for (i = SIGHUP; i <= SIGTERM; i++)
      signal (i, SIG_IGN);
}
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: