See how the COW finish the copy
About
The COW is Copy on Write,In my reading the Redis Database’s code process,i have had a problem:How redis to finish write in file when have other thread using the file?
Learning
Linux have some C function which namedfork() and exec(),for example,i will write some code to show how these function work.
1 | |
now we use gcc to build the out file and run it.
1 | |
we can see the shell have double 1,we use gdb testing it.
1 | |
in line six , GDB tell us the child process have been detach,we copied the parent process to the child process. After the content is output, the child process automatically disengages,until finish the testing and remove the main process from the stack.
In this process, although our program keeps running, a new process is also created. This is COW(copy on write). The child process(renamed B) is forked from the parent process(renamed A), but B shares A’s memory pages at first instead of copying all of them; only when one of them writes to a page does the system copy that page, so the two processes don’t interfere with each other.
In Redis,we need to make data persistent,But we couldn’t close the server, so we should use snapshotting , which is copy on write
Expand
except Copy on write,also have read on write(ROW)there are many similarities between the two. COW and ROW all used into snapshot technology