Can exit() fail to terminate process?

前端 未结 3 1299
我寻月下人不归
我寻月下人不归 2021-01-02 02:27

I have a registered a signal handler in my program. Upon receiving an undesired signal (SIGABRT), i call \'exit(-1)\' in signal handler to exit the process. But as noticed o

3条回答
  •  难免孤独
    2021-01-02 02:53

    Are you calling exit() from the signal handler?

    In man 7 signal, section Async-signal-safe functions you can see all the functions that are guaranteed to work when called from an signal handler:

    A signal handler function must be very careful, since processing elsewhere may be interrupted at some arbitrary point in the execution of the program. POSIX has the concept of "safe function". If a signal interrupts the execution of an unsafe function, and handler calls an unsafe function, then the behavior of the program is undefined.

    POSIX.1-2004 (also known as POSIX.1-2001 Technical Corrigendum 2) requires an implementation to guarantee that the following functions can be safely called inside a signal handler:

    There you can see functions _Exit(), _exit() and abort(), but notably not exit(). So you should not call it from a signal handler.

    The nasty thing is that even if you call an unsafe function from a signal handler (printf() any?) it will just work most of the time... but not always.

提交回复
热议问题