If I fork and exec another executable, will the newly spawned process be able to access memory shared through mmap from the parent.
...
fd = open(filename)
str = mmap (MAP_SHARED, .. fd)
pid = fork();
if(pid == 0) {
exec("executable_2");
}
....
My question is, is it possible to access (read only) the shared memory mapped from file, from this spawned executable_2?
EDIT: the main purpose would be to save reading time (I/O) since this file is read-only. The newly spawned process is not a copy of the calling process.
The child process would have to remap the memory to access it, but can do so unless the 'shared' memory was mapped privately.
This would apply to all forms of shared memory across an exec*()
— the new process has a new, independent address space and any shared memory mapping must be done anew in the executed process.
Simply forking, of course, leaves the shared memory as shared memory. But using exec*()
gives the new process a clean address space unsullied by shared memory from the program it was running as previously.
Note that since the file descriptor was not opened with O_CLOEXEC
(or was not later modified to set the FD_CLOEXEC flag on it), the file descriptor is open in the executed process. Whether the executed process knows what it is open for is another matter altogether — it probably doesn't unless it gets told by the code that executed it (command line argument, or perhaps environment variable).