What is correct way to have single Signal Handler function for multiple Signals?

前端 未结 3 1475
野趣味
野趣味 2021-01-02 07:03

What is the best way in C on Linux for setting up a program that can handle multiple POSIX-signals with the same function?

For example in my code I have a handler fu

相关标签:
3条回答
  • 2021-01-02 07:15

    Is this the correct way to set up this type of handling?

    Not quite - it's not safe to use printf() inside a signal handler, but you can still use write() to stdout or stderr.

    0 讨论(0)
  • 2021-01-02 07:20

    "signum" parameter of "sigaction" system call is an integer value, which does not work as a flag.

    As far as I know, there's no way to assign one handler function for several signals in one call.

    0 讨论(0)
  • 2021-01-02 07:30

    I can't see how you can straightforwardly set a single handler for all signals. However, you can get fairly close by using sigfillset() to generate a set containing all valid signal numbers, and then iterate over possible signal numbers using sigismember() to determine whether that number is in the set, and set a handler if so. OK, I can't see a method of determining what the maximum possible signal number is, so you might have to guess a suitable maximum value.

    0 讨论(0)
提交回复
热议问题