Skip to content

Instantly share code, notes, and snippets.

@jakekara
Created May 9, 2017 15:55
Show Gist options
  • Select an option

  • Save jakekara/b40a2235d92cb89be6c7971e37c7d38b to your computer and use it in GitHub Desktop.

Select an option

Save jakekara/b40a2235d92cb89be6c7971e37c7d38b to your computer and use it in GitHub Desktop.
Examining the return valu of signal function
/*
* sig.c - examining the return value of the signal function
*
* The signal function returns the pointer to the previous
* handler for the signal.
*/
#include <stdio.h> /* for printf */
#include <signal.h> /* for signal */
#include <stdlib.h> /* for exit */
#include <unistd.h>
typedef void (*sig_t) (int); /* A void function*/
static sig_t prev;
/* some_fun - a simple signal handler */
void some_fun()
{
printf("\nBye!\n");
exit(0);
}
/* set_handler - set the SIGINT handler and store the previous
* value
*/
void set_handler( sig_t handler )
{
prev = signal(SIGINT, handler);
printf("SIGINT was previously handled by %p ", prev);
printf("and should now be handled by %p\n", handler);
printf("3...");
fflush(stdout);
sleep(1);
printf("2...");
fflush(stdout);
sleep(1);
printf("1...");
fflush(stdout);
sleep(1);
printf("\n");
}
int main(){
printf("some_fun is at address: %p\n", some_fun);
printf("SIG_DFL is at addrss: %p\n", SIG_DFL);
set_handler(some_fun);
set_handler(prev);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment