为什么malloc()和printf()的说,非重入? [英] Why are malloc() and printf() said as non-reentrant?

查看:693
本文介绍了为什么malloc()和printf()的说,非重入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UNIX系统中,我们知道的malloc()是一个非重入函数(系统调用)。这是为什么?

In UNIX systems we know malloc() is a non-reentrant function (system call). Why is that?

同样,的printf()也被说成是不可重入;为什么?

Similarly, printf() also is said to be non-reentrant; why?

我知道重入的定义,但我想知道为什么它适用于这些功能。
  什么prevents他们所保证折返?

I know the definition of re-entrancy, but I wanted to know why it applies to these functions. What prevents them being guaranteed reentrant?

推荐答案

的malloc 的printf 通常采用全球结构,并采用基于锁的同步内部。这就是为什么他们不折返。

malloc and printf usually use global structures, and employ lock-based synchronization internally. That's why they're not reentrant.

的malloc 功能既可以是线程安全的或线程安全的。两者都是不可重入的:

The malloc function could either be thread-safe or thread-unsafe. Both are not reentrant:


  1. malloc的工作在全局堆,它可能是的malloc 这种情况发生在同一时间的两个不同的调用,返回相同的内存块。 (第二malloc调用应该发生前的块的地址是牵强,但大块未标记为不可用)。这违反的malloc 的后置条件,所以这个实施就不会重入的。

  1. Malloc operates on a global heap, and it's possible that two different invocations of malloc that happen at the same time, return the same memory block. (The 2nd malloc call should happen before an address of the chunk is fetched, but the chunk is not marked as unavailable). This violates the postcondition of malloc, so this implementation would not be re-entrant.

要prevent这种效果,的malloc的线程安全实施将使用基于锁的同步。但是,如果malloc的是从信号处理程序调用,下面的情况可能发生:

To prevent this effect, a thread-safe implementation of malloc would use lock-based synchronization. However, if malloc is called from signal handler, the following situation may happen:

malloc();            //initial call
  lock(memory_lock); //acquire lock inside malloc implementation
signal_handler();    //interrupt and process signal
malloc();            //call malloc() inside signal handler
  lock(memory_lock); //try to acquire lock in malloc implementation
  // DEADLOCK!  We wait for release of memory_lock, but 
  // it won't be released because the original malloc call is interrupted

这不会发生的情况当的malloc 只是从不同的线程调用。事实上,重入概念已经超越线程安全,也需要功能正常工作即使它调用的一个永远不会终止。这基本上是推理为什么与任何锁功能会不会重入的。

This situation won't happen when malloc is simply called from different threads. Indeed, the reentrancy concept goes beyond thread-safety and also requires functions to work properly even if one of its invocation never terminates. That's basically the reasoning why any function with locks would be not re-entrant.

的printf 函数也对全球数据的操作。任何输出流通常使用附连到被发送到资源数据的全局缓冲器(为终端,或为一个文件缓冲器)。打印过程通常是将数据复制到缓冲事后冲洗缓冲器的序列。 的malloc 做这个缓冲区应该由锁以同样的方式进行保护。因此,的printf 也是非重入。

The printf function also operated on global data. Any output stream usually employs a global buffer attached to the resource data are sent to (a buffer for terminal, or for a file). The print process is usually a sequence of copying data to buffer and flushing the buffer afterwards. This buffer should be protected by locks in the same way malloc does. Therefore, printf is also non-reentrant.

这篇关于为什么malloc()和printf()的说,非重入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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