对一段数据进行一次更改后,是否所有标记为写时复制的内存都会被复制?

发布于 2024-10-13 00:42:14 字数 410 浏览 9 评论 0原文

我的问题也许是一个措辞不当的问题,源于我对内存管理的业余理解。

我担心的是:我有一个 Perl 脚本,它分叉了很多次。据我从 perldoc 的 fork 页面了解到,正在实现写时复制。然后,每个子进程调用 system(),再次分叉,以调用外部程序。来自外部程序的数据被读回到子程序中,并转储为可存储文件,以便在所有子程序退出后由父程序获取和处理。

我担心的是我对这种情况的感知波动。考虑一下我脑海中看到的最坏的情况: 对于每个子级,一旦新数据到达,整个写时复制内存就会被复制。如果是这种情况,我在创建几个分叉后很快就会遇到内存问题。

但另一方面,写时复制是否只复制包含所需数据的最小内存块?那么这个内存量子是什么呢?它的大小是如何设定的?

我不确定我所要求的细节是否依赖于语言或依赖于某些较低级别的过程。

My question is, perhaps, a poorly worded one and stems from my amateurish understanding of memory management.

My concern is this: I have a Perl script that forks many times. As I understand from the fork page in perldoc, copy-on-write is being implemented. Each of the children then calls system(), forking again, to call an external program. The data from the external program is read back into the child, and dumped as a Storable file to be reaped and processed by the parent once all children have exited.

What concerns me is my perceived volatility of this situation. Consider, what I see in my mind, the worst case scenario:
For each of the children, as soon as new data arrives, the entire copy-on-write memory becomes, well, copied. If this is the case, I am going to quickly run into memory problems after creating a few forks.

But alternatively, does copy-on-write only copy the smallest chunk of memory that contains the needed data? Then what is this quanta of memory? How is its size set?

I am uncertain as to whether the specifics of what I am asking are language dependent or dependent on some lower-level process.

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

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

发布评论

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

评论(2

裸钻 2024-10-20 00:42:14

是的,分叉会增加你的内存占用。如果这是一个问题,请使用类似 Parallel::ProcManager< 的模块/a> 或 Forks::Super 可以限制活动后台进程的数量。当您的进程受 CPU 限制、I/O 限制或有可能过度使用计算机上的任何其他有限资源时,限制活动分叉的数量也是一个好主意。

use Forks::Super MAX_PROC => 10, ON_BUSY => block;

...
$pid = fork();        # blocks if there are already 10 child processes
...                   # unblocks when one of the children finishes

Yes, forking will increase your memory footprint. If this is a problem, use a module like Parallel::ProcManager or Forks::Super that can throttle the number of active background processes. Limiting the number of active forks is also a good idea when your processes are CPU bound, I/O bound, or have the potential to overuse any other limited resource on your machine.

use Forks::Super MAX_PROC => 10, ON_BUSY => block;

...
$pid = fork();        # blocks if there are already 10 child processes
...                   # unblocks when one of the children finishes
风筝有风,海豚有海 2024-10-20 00:42:14

内存按页组织,通常每个页 4K(这可以设置为不同的值,并且是特定于硬件的,但这是具有标准操作系统的 Intel 平台上的规范)。当子进程写入写时复制页面时,它将被复制。

Memory is organized in pages, generally 4K each (this can be set to different values, and is hardware-specific, but that's the norm on Intel platforms with standard operating systems). When a child process writes into a copy-on-write page, it will be copied.

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