Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy-on-write forking in Ruby

Tags:

fork

ruby

I'm learning about process forking, but also how it's accomplished in Ruby specifically. In my reading I've learned about 'copy-on-write' behavior. From what I understand this basically makes no copy of memory until the child thread attempts to make a write operation. Does this mean that when the child process does attempt to make a write that the parent memory is copied and modified, and that the original memory is left unchanged (and thus the parent memory is unaffected by the child process?). Thanks.

like image 944
dreadwail Avatar asked Feb 18 '14 18:02

dreadwail


1 Answers

Yes. Copy-on-write means that only memory pages that are the same for both processes stay shared. If the child or parent process writes to one of the shared pages, the write will be intercepted, the page will be copied, and the write will occur on the new page that is owned solely by the child/parent process.

It's worth mentioning that this has nothing at all to do with Ruby specifically. Ruby uses the underlying system's fork system call, which on Linux and most UNIXes has copy on write behavior.

like image 54
Linuxios Avatar answered Sep 25 '22 14:09

Linuxios