获取fork()的ING的写入时复制行为,不叉() [英] Get the copy-on-write behaviour of fork()ing, without fork()

查看:98
本文介绍了获取fork()的ING的写入时复制行为,不叉()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的缓冲区:

char *buf = malloc(1000000000); // 1GB

如果我分叉的一个新的进程,这将有哪些共享与父母的BUF内存中,直到一方或另一方写信给它一个buf中。即使这样,只有一个新的4KiB块需要由内核分配,其余将继续分享。

If I forked a new process, it would have a buf which shared memory with the parent's buf until one or the other wrote to it. Even then, only one new 4KiB block would need to be allocated by the kernel, the rest would continue to be shared.

我想作BUF的副本,但我只打算改变一点点的副本。我想写入时复制行为,而分叉。 (就像你得到分叉时免费的。)

I'd like to make a copy of buf, but I'm only going to change a little of the copy. I'd like copy-on-write behaviour without forking. (Like you get for free when forking.)

这可能吗?

推荐答案

您会希望在磁盘上创建或文件中的POSIX共享内存段(的shm_open )的该块。第一次,用 MAP_SHARED 映射。当你准备做一个副本,并切换到牛,呼 MMAP MAP_FIXED 和<$ C $再次C> MAP_PRIVATE 你原来的地图之上,并与 MAP_PRIVATE 地图上,使第二个副本。这应该得到你想要的效果。

You'll want to create a file on disk or a POSIX shared memory segment (shm_open) for the block. The first time, map it with MAP_SHARED. When you're ready to make a copy and switch to COW, call mmap again with MAP_FIXED and MAP_PRIVATE to map over top of your original map, and with MAP_PRIVATE to make the second copy. This should get you the effects you want.

这篇关于获取fork()的ING的写入时复制行为,不叉()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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