为什么 malloc 不是异步信号安全的? [英] Why is malloc not async signal safe?

查看:29
本文介绍了为什么 malloc 不是异步信号安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能在信号处理程序中使用 malloc?什么会发生错误"?

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

推荐答案

可以随时调用信号处理程序,包括在对 malloc 的另一个调用正在进行时.如果发生这种情况,将会发生以下两种情况之一:

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. 您的进程将在信号处理程序中死锁,因为 malloc 将无法获取堆锁.
  2. 你的进程会破坏它的堆,因为 malloc 确实获取了锁(或者认为它不需要它),然后继续渲染堆不一致,导致到后来的崩溃.
  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.

这篇关于为什么 malloc 不是异步信号安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆