Skip to content

Instantly share code, notes, and snippets.

@richiejp
Created January 10, 2012 18:37
Show Gist options
  • Save richiejp/1590423 to your computer and use it in GitHub Desktop.
Save richiejp/1590423 to your computer and use it in GitHub Desktop.
POSIX signal example I wrote for my blog
/*Really Simple POSIX Signal example.
Copyright (c) 2006-2007 Richard Palethorpe
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
Website: richiejp.wordpress.com email: richiejp@gmail.com*/
/*Include stdlib for EXIT_SUCCESS/FAILURE defines and exit function*/
#include <stdlib.h>
/*Include stdio.h for the puts function*/
#include <stdio.h>
/*Include signal.h for sigaction method/struct, raise method and SIGTERM define*/
#include <signal.h>
void regHand(int, void (*func)(int));
void handler(int);
int main(){
/*Register handler (callback method) for the termination signal (SIGTERM)
using the method regHand which is defined below*/
regHand(SIGTERM, handler);
/*Send SIGTERM to ourselves using raise*/
raise(SIGTERM);
/*This code should never be exucted*/
puts("Have raised signal, but still in main, something is wrong.");
return EXIT_FAILURE;
}
/*This method registers handler method which is executed when a signal of
signalType is sent to this process*/
void regHand(int signalType, void (*func)(int)){
puts("In the regHand function");
/*A struct used to store information about the action performed when a signal
is recieved by this process*/
struct sigaction sa;
/*The handler which is executed when the signal is recieved. You can use two
premade handlers called SIG_DFL for default and SIG_IGN for ignore.*/
sa.sa_handler = func;
/*Another type of handler which recieves info about the signal through two
extra parameters, you can't use both sa_handler and this*/
//sa.sa_sigaction = NULL;
/*A list of signals to block when the handler is being executed*/
//sa.sa_mask = NULL;
/*Various options concerning the way in which signals are processed*/
sa.sa_flags = 0;
/*A callback that isn't used anymore*/
sa.sa_restorer = NULL;
/*Register the handler using sigaction*/
sigaction(signalType, &sa, NULL);
}
/*The callback function that is called when the signal is recieved*/
void handler(int status){
puts("In the handler function");
/*The SIGTERM signal tells a process to exit, so we exit. You can't make a
process invincible by ignoreing SIGTERM as there are still the
SIGKILL and SIGSTOP signals which you can't set a handler for (catch) or
ignore*/
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment