Why is malloc not async signal safe?

后端 未结 1 416
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 11:39

Why can\'t malloc be used in signal handlers? What can \"happen wrong\"?

相关标签:
1条回答
  • 2020-12-10 12:09

    A signal handler can be called at any time, including during times when another call to malloc is in progress. If this happens, one of two things will occur:

    1. Your process will deadlock inside the signal handler, because malloc will be unable to acquire the heap lock.
    2. Your process will corrupt its heap, because malloc does acquire the lock (or doesn't think it needs it), then proceeds to render the heap inconsistent, leading to a later crash.
    0 讨论(0)
提交回复
热议问题