Simple Linux Signal Handling

后端 未结 5 552
天涯浪人
天涯浪人 2020-11-30 00:49

I have a program that creates many threads and runs until either power is shutdown to the embedded computer, or the user uses kill or ctrlc

5条回答
  •  再見小時候
    2020-11-30 01:17

    First of all - if you don't know, whether you should handle any signals, then you probably don't. Signals require handling in some specific cases, like closing sockets, sending some message to other connected processes before exiting, or handling SIGPIPE signal from write() (and probably many more).

    Secondly - this while (!terminate) sleep(2); is not very good - in worst case it might make user (or even system) impatient with having to wait 2s and send to your program a SIGKILL, which you can't handle.

    IMHO the best solution here would be using signalfd and select, so you can terminate your program without having to wait 2s.

提交回复
热议问题