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?
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 tounlink(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: