It's to simplify the interface. The alternative to fork
and exec
would be something like Windows' CreateProcess function. Notice how many parameters CreateProcess
has, and many of them are structs with even more parameters. This is because everything you might want to control about the new process has to be passed to CreateProcess
. In fact, CreateProcess
doesn't have enough parameters, so Microsoft had to add CreateProcessAsUser and CreateProcessWithLogonW.
With the fork/exec
model, you don't need all those parameters. Instead, certain attributes of the process are preserved across exec
. This allows you to fork
, then change whatever process attributes you want (using the same functions you'd use normally), and then exec
. In Linux, fork
has no parameters, and execve
has only 3: the program to run, the command line to give it, and its environment. (There are other exec
functions, but they're just wrappers around execve
provided by the C library to simplify common use cases.)
If you want to start a process with a different current directory: fork
, chdir
, exec
.
If you want to redirect stdin/stdout: fork
, close/open files, exec
.
If you want to switch users: fork
, setuid
, exec
.
All these things can be combined as needed. If somebody comes up with a new kind of process attribute, you don't have to change fork
and exec
.
As larsks mentioned, most modern Unixes use copy-on-write, so fork
doesn't involve significant overhead.
RSS is how much memory this process currently has in main memory (RAM). VSZ is how much virtual memory the process has in total. This includes all types of memory, both in RAM and swapped out. These numbers can get skewed because they also include shared libraries and other types of memory. You can have five hundred instances of bash
running, and the total size of their memory footprint won't be the sum of their RSS or VSZ values.
If you need to get a more detailed idea about the memory footprint of a process, you have some options. You can go through /proc/$PID/map
and weed out the stuff you don't like. If it's shared libraries, the calculation could get complex depending on your needs (which I think I remember).
If you only care about the heap size of the process, you can always just parse the [heap]
entry in the map
file. The size the kernel has allocated for the process heap may or may not reflect the exact number of bytes the process has asked to be allocated. There are minute details, kernel internals and optimisations which can throw this off. In an ideal world, it'll be as much as your process needs, rounded up to the nearest multiple of the system page size (getconf PAGESIZE
will tell you what it is — on PCs, it's probably 4,096 bytes).
If you want to see how much memory a process has allocated, one of the best ways is to forgo the kernel-side metrics. Instead, you instrument the C library's heap memory (de)allocation functions with the LD_PRELOAD
mechanism. Personally, I slightly abuse valgrind
to get information about this sort of thing. (Note that applying the instrumentation will require restarting the process.)
Please note, since you may also be benchmarking runtimes, that valgrind
will make your programs very slightly slower (but probably within your tolerances).
Best Answer
In modern systems none of the memory is actually copied just because a fork system call is used. It is all marked read only in the page table such that on first attempt to write a trap into kernel code will happen. Only once the first process attempt to write will the copying happen.
This is known as copy-on-write.
However it may be necessary to keep track of committed address space as well. If no memory or swap is available at the time the kernel has to copy a page, it has to kill some process to free memory. This is not always desirable, so it is possible to keep track of how much memory the kernel has committed to.
If the kernel would commit to more than the available memory + swap, it can give an error code on attempt to call fork. If enough is available the kernel will commit to the full virtual size of the parent for both processes after the fork.