Created
October 17, 2022 20:17
-
-
Save adililhan/301773b7efd3eeb0da745ef9a6aa10ba to your computer and use it in GitHub Desktop.
reentrant function
This file contains 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 <unistd.h> | |
#include <stdlib.h> | |
#include <signal.h> | |
int calculate(int first, int second) { | |
int sum = first + second; | |
sleep(3); // represent I/O intensive function call | |
return sum; | |
} | |
void signal_handler(int signal_number) { | |
int sum = 50 + 60; | |
printf("Interrupt caught!\n"); | |
printf("Result from signal_handler: %d\n", sum); | |
printf("---\n"); | |
} | |
int main() | |
{ | |
struct sigaction sa; | |
sa.sa_handler = signal_handler; | |
sa.sa_flags = SA_RESTART; | |
if(sigaction(SIGINT, &sa, NULL) == -1) { | |
perror("sigaction"); | |
exit(EXIT_FAILURE); | |
} | |
for (;;) { | |
printf("Result from main: %d\n", calculate(3, 5)); | |
printf("---\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment