Created
November 3, 2015 02:12
-
-
Save goyusia/3ee77ed16a900b74922b to your computer and use it in GitHub Desktop.
fibonacci with signal handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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