什么是可重入程序?

发布于 2024-11-29 10:55:15 字数 136 浏览 0 评论 0原文

什么是可重入过程?您能否给出使用它的示例场景?

编辑:另外,多个进程可以并行访问可重入过程吗?

请提供与维基百科不同的解释方式,因为我不完全理解他们的描述,因此我的问题在这里

What is a re entrant procedure and can you give an example scenario of when it is used?

Edit: Also, can multiple processes access a re entrant procedure in parallel?

Please provide a different way of explaining than wikipedia as I don't totally understand their description hence my question here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撞了怀 2024-12-06 10:55:15

重入背后的想法是,例程可以在执行过程中被调用,并且仍然可以正常工作。

一般来说,这是通过仅使用在堆栈上声明的参数和局部变量(用 C 术语来说,没有静态局部变量)来实现的。同样重要的是,它在执行期间不锁定任何全局资源。

现在,您可能会问,“例程同时运行多次这样奇怪的事情怎么会发生呢?”嗯,可能发生这种情况的一些方式是:

  • 例程是递归的(或与其他一些例程集相互递归)。
  • 它被另一个线程调用。
  • 它被中断调用。

如果发生其中任何一种情况,并且例程正在修改全局(或 C 静态本地),则新的执行可能会消除第一次执行所做的更改。例如,如果将该全局变量用作循环控制变量,则可能会导致第一次执行(当它最终恢复时)循环错误的次数。

The idea behind re-entrancy is that the routine may be called while it is in the middle of executing already and it will still work right.

Generally this is achieved by it using only parameters and local variables declared on the stack (in C terms, no static locals). It would also be important that it not lock any global resources during execution.

Now, you may ask, "How would such a weird thing as a routine being run multiple times at once happen?" Well, some ways this could happen are:

  • The routine is recursive (or mutually-recursive with some other set of routines).
  • It gets called by another thread.
  • It gets called by an interrupt.

If any of these happen, and the routine is modifying a global (or C static local), then the new execution could potentially wipe out the changes the first execution made. As an example, if that global was used as a loop control variable, it might cause the first execution, when it finally gets to resume, to loop the wrong number of times.

别低头,皇冠会掉 2024-12-06 10:55:15

它是一个子例程,可以在已经激活时调用。例如,递归函数通常是可重入的。从信号处理程序调用的函数也必须是可重入的。可重入函数是线程安全的,但并非所有线程安全的函数都是可重入的。

It is a subroutine which can be called when it is already active. For instance recursive functions are often reentrant. Functions which are called from signal handlers must be reentrant as well. A reentrant function is thread-safe but not all thread-safe one are reentrant.

触ぅ动初心 2024-12-06 10:55:15

可重入过程是一种程序代码的单个副本可以在同一时间段内由多个用户共享的过程。重新进入有两个关键方面:程序代码不能自行修改以及每个用户的本地数据必须单独存储。

在共享系统中,可重入性可以更有效地使用主内存:程序代码的一份副本保留在主内存中,但多个应用程序可以调用该过程。因此,可重入过程必须具有永久部分(组成过程的指令)和临时部分(返回调用程序的指针以及程序使用的局部变量的内存)。

过程的每个执行实例(称为激活)都将执行永久部分中的代码,但必须有自己的局部变量和参数副本。与特定激活相关联的临时部分称为激活记录。

支持可重入过程的最方便的方法是使用堆栈。当调用可重入过程时,活动记录成为在过程调用时创建的堆栈帧的一部分

A reentrant procedure is one in which a single copy of the program code can be shared by multiple users during the same period of time. Re entrance has two key aspects: The program code cannot modify itself and the local data for each user must be stored separately.

In a shared system, reentrancy allows more efficient use of main memory: One copy of the program code is kept in main memory, but more than one application can call the procedure. Thus, a reentrant procedure must have a permanent part( the instructions that make up the procedure) and a temporary part(a pointer back to the calling program as well as memory for local variables used by the program).

Each execution instance, called activation, of a procedure will execute the code in the permanent part but must have its own copy of local variables and parameters. The temporary part associated with a particular activation is referred to as an activation record.

The most convenient way to support reentrant procedures is by means of a stack. When a reentrant procedure is called, the activation record becomes part of the stack frame that is created on procedure call

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文