1

Manpage of execve() says

execve() executes the program pointed to by filename. This causes the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and (initialized and uninitialized) data segments.

In case that the calling process was created by vfork(), it means that exec() provides a new address space for the new program; it doesn’t modify the parent address space..

If the calling process was created more generally by fork(), what normally happens to the old "stack, heap, and (initialized and uninitialized) data segments"? Are their spaces deallocated?

Does the behavior of execve() depend on how the calling process was created?

Before seeing the quote, I thought that execve() would overwrite the old "stack, heap, and (initialized and uninitialized) data segments", instead of creating new ones. So when I saw the quote, wonder why waste the new space?

Thanks.

2

Does the behavior of execve() depend on how the calling process was created?

No.

Before seeing the quote, I thought that execve() would overwrite the old "stack, heap, and (initialized and uninitialized) data segments", instead of creating new ones.

No: execve() creates new segments and deallocates the old ones.

So when I saw the quote, wonder why waste the new space?

Try to remember, we are talking about virtual memory!

Creating a blank segment only allocates a small amount of physical memory, to track the segment. The size of that allocation is fixed, no matter how large the segment is.

A physical page must be allocated for each page you write to. The parent process might have written to a large number of pages. But if the child only uses a little stack/heap/data when it runs, there is no good reason for it to keep references to all the dirty pages from the parent! It will waste memory if the parent exits and the child continues running.

Removing the references to the old segments and the old pages is the most efficient approach. Because if that was the only reference to that memory, the physical memory can be deallocated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.