Skip to content

Instantly share code, notes, and snippets.

@adililhan
Created October 17, 2022 20:15
Show Gist options
  • Save adililhan/da17d2240791e59a17dac33f526b614d to your computer and use it in GitHub Desktop.
Save adililhan/da17d2240791e59a17dac33f526b614d to your computer and use it in GitHub Desktop.

Revisions

  1. adililhan created this gist Oct 17, 2022.
    39 changes: 39 additions & 0 deletions non-reentrant-sigaction-local-variable.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <signal.h>

    int sum;

    int calculate(int first, int second) {
    sum = first + second;
    sleep(3); // represent I/O intensive function call
    return sum;
    }


    void signal_handler(int signal_number) {
    int sum_internal = 50 + 60;
    printf("Interrupt caught!\n");
    printf("Result from signal_handler: %d\n", sum_internal);
    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;
    }