We have an assignment to explain this text of code. My only problem is understanding the handle_signal function, why did we use 2 new sigaction and then used "old_treatment" with "rien"?
#define DELAY 1 #define NB_ITERATIONS 60 void handle_signal (int num_signal){ struct sigaction rien, old_ treatment; printf ("Signal %d => ", num_signal); printf ("I have received a SIGTSTP.\n"); rien.sa_handler = SIG_DFL; rien.sa_flags = 0; sigemptyset (&rien.sa_mask); sigaction (SIGTSTP, &rien, &old_ treatment); printf ("Then I sleep....\n"); kill (getpid(), SIGSTOP); printf ("They wakes me?\n"); Sigaction (SIGTSTP, &old_ treatment, NULL); printf ("Here we go again!\n"); } int main (void){ struct sigaction a; int i; a.sa_handler = handle_signal; sigemptyset (&a.sa_mask); sigaction (SIGTSTP, &a, NULL); for (i = 1; i < NB_ITERATIONS; i++) { sleep (DELAY); printf ("%d", i % 10); fflush (stdout);} printf ("End\n"); return EXIT_SUCCESS; }
printf()is not async-signal-safe. See pubs.opengroup.org/onlinepubs/9699919799/functions/…case, and noswitchsigactionto mixed caseSigaction, not aboutcaseandswitchstatements.