Search code examples
memory-managementposixreal-time

POSIX compliant way to walk process memory


Is there a way for a process to walk it's own memory that is POSIX compliant? This must be done without modifying the process's source code, but we can interpose on system calls.

The application is for real-time systems which need to prevent unexpected delays from copy-on-write after a fork(). A solution is to call mlockall(), and then perform dummy writes to all memory that the process has mapped. In Linux, memory maps are available in /proc/self/maps (or /proc//maps), but this is not a requirement for POSIX compatibility.


Solution

  • In POSIX, I could only find posix_madvise(), which requires a given memory region as input. Also, posix_madvise() does not specify if it will affect the performance when writing and seems to work only on memory mapped files.

    I believe this is a simple matter of building yourself a list of allocated data and then performing the writes. I've also seen recommendation to allocate a large dummy stack and fill it with zeros, so that stack variable access won't block. If I understood the problem, you don't need and should not copy program code.

    With POSIX you won't be able to find and load pages that were allocated without program control.