How can I share memory between the parent and the child if the child has run exec() to load another program?
Is it possible using mmap?
By now parent and child share memory properly using mmap, but not after exec is done
How can I share memory between the parent and the child if the child has run exec() to load another program?
Is it possible using mmap?
By now parent and child share memory properly using mmap, but not after exec is done
Neither memory mappings created via
mmap()
nor POSIX shared-memory segments obtained viashm_open()
nor System V shared-memory segments obtained viashmat()
are preserved across an exec. That covers all the forms of shared memory I am aware of Linux providing.It follows that if you want the child to share memory with the parent after an
exec()
then the child must (re)connect to the appropriate shared memory after theexec()
. Obviously, that means that whatever program is started by theexec()
must be written to do that. (Note in particular that on success, the exec-family functions do not return.)Only if the child re-maps the memory after the
exec()
.That is by design.
You can use
shm_open
to open a "named" shared memory block which is identified by a file on the filesystem. Example: In the parent:In the other file:
After these segments of code you can use
buffer
to access the shared memory. (Note: it doesn't need to bevoid*
, you can make it a pointer to whatever you intend to store in the shared mem)