This signal is generated when a process attempts to access a region of a mapping that is no longer valid—for example, because the file was truncated after it was mapped.
This signal is generated when a process attempts to write to a region that is mapped read-only.
Linux provides the munmap() system call for removing a mapping created with mmap():
int munmap (void *addr, size_t len);
A call to munmap() removes any mappings that contain pages located anywhere in the process address space starting at addr, which must be page-aligned, and continuing forlenbytes. Once the mapping has been removed, the previously associated memory region is no longer valid, and further access attempts result in aSIGSEGVsignal.
Normally,munmap()is passed the return value and thelenparameter from a previous invocation ofmmap().
On success,munmap()returns0; on failure, it returns -1, anderrnois set appropriately. The only standarderrnovalue isEINVAL, which specifies that one or more parameters were invalid.
As an example, the following snippet unmaps any memory regions with pages contained in the interval[addr,addr+len]:
if (munmap (addr, len) == -1) perror ("munmap");
Please check back next week for the continuation of this article.