Get the copy-on-write behaviour of fork()ing, without fork()

前端 未结 1 638

I have a large buffer:

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

If I forked a new process, it would have a buf which shared memory with the p

相关标签:
1条回答
  • 2020-12-16 04:35

    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.

    0 讨论(0)
提交回复
热议问题