user3393951
user3393951

Reputation: 21

fork system call: Does the parent's preallocated memory gets allocated again for the children?

int *g=NULL;

main()
{

g = malloc(40000000);//allocate 40MB in the parent

fork();

while(1);

}

If I run this program and watch the same in the top, I see the 40MB memory allocated against the parent and also the child.

But in the background I expected the copy on write to prevent the double allocation of memory, because I have not touched it yet..

Comments? Is COW specific to some platforms? How can I test if COW is indeed the way for fork?

Upvotes: 2

Views: 117

Answers (1)

JaredPar
JaredPar

Reputation: 755397

Logically the child process has the 40MB of memory allocated to it, it just doesn't get a separate copy until it modifies it.

The COW operation doesn't change the amount of resources that are allocated to a process. Instead it just prevents physically duplicating them until an edit is made at which point the new process needs a separate copy.

Upvotes: 4

Related Questions