- 论坛徽章:
- 0
|
原帖由 yaoaiguo 于 2007-11-23 09:39 发表 ![]()
这样的问题当然存在,下面是测试的代码,用fork会失败,用vfork会成功,当然我只实验了一两次,因为我觉得这种情况,vfork也有可能失败。
#include
#include
#define LARGE_MEM 1024 * 1024 * 1024
int m ...
我在机器上运行, fork, vfork都不会出错.
下面是我机器的配置
total used free shared buffers cached
Mem: 3107224 918352 2188872 0 176392 624696
-/+ buffers/cache: 117264 2989960
Swap: 2096472 0 2096472
下面这段话摘自<Linux Kernel Development>, 是讲的写时复制的内容, 您看看.
Copy-on-Write
Traditionally, upon fork() all resources owned by the parent are duplicated and the copy is given to the child. This approach is significantly naïve and inefficient in that it copies much data that might otherwise be shared. Worse still, if the new process were to immediately execute a new image, all that copying would go to waste. In Linux, fork() is implemented through the use of copy-on-write pages. Copy-on-write (or COW) is a technique to delay or altogether prevent copying of the data. Rather than duplicate the process address space, the parent and the child can share a single copy. The data, however, is marked in such a way that if it is written to, a duplicate is made and each process receives a unique copy. Consequently, the duplication of resources occurs only when they are written; until then, they are shared read-only. This technique delays the copying of each page in the address space until it is actually written to. In the case that the pages are never writtenfor example, if exec() is called immediately after fork()they never need to be copied. The only overhead incurred by fork() is the duplication of the parent's page tables and the creation of a unique process descriptor for the child. In the common case that a process executes a new executable image immediately after forking, this optimization prevents the wasted copying of large amounts of data (with the address space, easily tens of megabytes). This is an important optimization because the Unix philosophy encourages quick process execution. |
|