What is copy-on-write fork, and why can Linux and Mac do it but not Windows?

62 viewsOtherTechnology

In a game I play called Factorio, whenever the game is saved the entire game momentarily freezes so that exact version of the level can be saved to disk. There’s an optional saving method exclusive to Linux/Mac versions of the game called non-blocking saving which eliminates this freeze, apparently by using a kernel feature called copy-on-write fork which I think duplicates the process in memory and that duplicate is then used for saving? Meaning the live version being played never has to freeze. What’s unique about this copy-on-write fork process that has prevented it from making its way to Windows?

In: Technology

2 Answers

Anonymous 0 Comments

Windows does not support the fork() command so the game devs would need to implement CoW fork-like functionality themselves rather than use a pre-provided functionality.

Anonymous 0 Comments

So there are two parts of this.

# 1. What is fork()?

fork() is one of the basic system calls in Unix systems (Mac, Linux). A system call is a request to the operating system to do something, usually programs are given a little sandbox to do their own thing in but if they have to do anything more complicated they have to ask the operating system to do it for them.

Fork is one of the basic system calls. It duplicates the running program, with only the minor difference of returning different values to the new and old process. Fork is very common, though most commonly it’s used as a “fork and exec” thing, combined with another syscall called exec which executed file program (this is for example generally what happens when you double click on a program and it runs).

To do this, fork needs to duplicate the entire memory (RAM) of the current process and depending on how much memory is used this may take a bit.

In Factorio’s case, it will fork to create a new process whose job it is to save the current state to file. Since it has the entire memory of the current state of the Factorio world, it can do that.

The problem is this is slow. Factorio keeps the whole world in memory and this takes up at least maybe a few megabytes to multiple gigabytes depending on the world, copying this even in ram isn’t instant.

Which leads us to a second thing, copy on write.

# 2. What is copy on write?

Copy on write is a strategy used in a number of different programming applications, the idea being that if we have to copy something, a file, memory, etc, you don’t actually need to copy it till someone writes to it, you can just have the two instances point to the same location in memory or disk. This way, the immediate time to “copy” is practically nothing, but the actual impact of copying is delayed till later, if it’s needed at all?

# 3. What does copy on write have to do with fork?

Well you might see this coming, but we don’t actually copy the memory. In modern operating systems, memory is divided into what we call pages, chunks of a few thousand bytes that we individually keep track of and allocate to a program. We can give the same memory to two programs by giving them the same page which maps to the same place in memory. But we can also give that page a “copy on write” flag to say “if you write to this, you need to ask the operating system to figure out how to copy this page.”

This reduces the immediate performance penalty of fork bigly, the fork happens instantly as it no longer has to copy the page. Future memory writes will suffer a small one time penalty in say changes to the game world but it’s mostly unnoticeable and most pages won’t be written to by the time the child process saves the world.

Why windows doesn’t have this? No clue.