2

Suppose in main I register 2 signal handlers for SIGUSR1 and SIGUR2, let's say sig_ur1 and sig_ur2. What's gonna happen when signal SIGUR2 arrives when sig_ur1 is running?

sig_ur1 running ----> signal SIGUR2 arrives ----> :

  1. sig_ur1 keeps running -> sig_ur1 finishes -> sig_ur2 starts to run -> sig_ur2 finishes -> return to main

  2. sig_ur2 starts to run -> sig_ur2 finishes -> return to main

  3. sig_ur2 starts to run -> sig_ur2 finishes -> sig_ur1 restores(variables keep the same value when sig_ur1 left) -> sig_ur1 finishes -> return to main

  4. sig_ur2 starts to run -> sig_ur2 finishes -> sig_ur1 restarts(all variables are initialized again) -> sig_ur1 finishes -> return to main

Which option is the correct one? Or neither is correct? I did not find any articles/posts talking about this, so providing any related material/links would be much appreciated. :)

Rick
  • 1,157

1 Answers1

1

By default scenario 3 is what happens. However you may make provision for blocking SIGUSR2 whenever running SIGUSR1’s handler, and thus you’d have scenario 1 occurring. When you install a signal’s handler you can specify which signals you want to block while running that signal’s handler.

As reference I would recommend you reading your operating system’s man-pages regarding signals. For instance on Linux you might start from man 7 signal and from there the SEE ALSO man-pages.

However you can easily check the behavior by yourself: just make a handler for SIGUSR1 that goes to a long sleep(3) (in Linux better nanosleep(2) because sleep(3) might be implemented through SIGALRM), then send first SIGUSR1 and then SIGUSR2.

LL3
  • 5,418