user1018513
user1018513

Reputation: 1682

Clone, fork, vfork behaviour when followed by an exec

I am curious about the behaviour of calling clone from a process with most of the flags set (so that the two processes share an execution context, namely share the address space, file descriptor talbe, etc). I wasn't able to fully answer my question using the material online.

Now assuming I call execve from the cloned process. This replaces the image of the process with a completely new one, destroys attached memory segments, discards allocated memory etc, but what happens to the parent process? Given these share (for example, all the malloced memory regions), do they also get deallocated?

Does the new execved process still share an address space with the parent?

Upvotes: 3

Views: 802

Answers (2)

Paul Irofti
Paul Irofti

Reputation: 432

The shared objects are unmapped or unlinked but from a shared perspective.

Say you have 3 processes/threads all of them sharing memory starting at 0x1000.

One of them does an execve. Then it will do an shm_unlink(2) on 0x1000. shm_unlink(2) will try to unlink(2) it.

Now for each process/thread using that memory range there is a counter. In our case the counter is set to 3 before the execve(2) and it will be set to 2 after it. No memory loss.

The memory will be 'destroyed', as you put it, when no process is using it anymore. When the counter is 0.

Same goes for all shared objects. For a list of what system calls are called and how they're 'destroying' the shared objects have a look at the links in the execve(2) manpage. Search for this phrase:

All process attributes are preserved during an execve(), except the following

Upvotes: 1

Jens
Jens

Reputation: 72657

The parent process is unaffected by the execve. It's a different process after all. Processes always must use some explicit method to communicate (files, pipes, IPC like shared memory, signals, ...). Since all of this is destroyed in a child performing an exec, there's no communication possible until explicitly set up again.

Upvotes: 0

Related Questions