Skip to content

Instantly share code, notes, and snippets.

@goyusia
Created November 3, 2015 02:12
Show Gist options
  • Select an option

  • Save goyusia/3ee77ed16a900b74922b to your computer and use it in GitHub Desktop.

Select an option

Save goyusia/3ee77ed16a900b74922b to your computer and use it in GitHub Desktop.
fibonacci with signal handler
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
static int prev_fib = 0;
static int curr_fib = 1;
void sig_handler(int signo)
{
if (signo == SIGINT) {
sigset(SIGINT, SIG_IGN);
main();
}
}
int main(void)
{
printf("fib: %d\n", curr_fib);
int next_fib = curr_fib + prev_fib;
prev_fib = curr_fib;
curr_fib = next_fib;
if (signal(SIGINT, sig_handler) == SIG_ERR) {
printf("\ncan't catch SIGINT\n");
}
printf("ctrl-c to calculate next fibonacci\n");
// A long long wait so that we can easily issue a signal to this process
while(1)
sleep(1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment