Destroying Memory Mappings

With the exception of the MAP_INHERIT flag for FreeBSD, the memory-mapped regions are unmapped automatically by the kernel when execve(2) is called or when the process terminates. It may occur in an application, however, that the memory-mapped file is needed only temporarily. The munmap(2) system call is used to unmap it:

#include <sys/types.h>
#include <sys/mman.h>

int munmap(void *addr, size_t len);

The memory region to be unmapped is specified as the region starting at addr for a length of len bytes. The function munmap(2) returns 0 when successful. Otherwise, -1 is returned, with an error code left in errno. It should be noted that this system call does not cause pending changes to be written out to the file. ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.