The MMAP System Call in Linux - Associated signals (
Page 4 of 4 )
Two signals are associated with mapped regions:
SIGBUS
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.
SIGSEGV
This signal is generated when a process attempts to write to a region that is mapped read-only.
munmap()
Linux provides the munmap() system call for removing a mapping created with mmap():
#include <sys/mman.h>
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 for
len
bytes. Once the mapping has been removed, the previously associated mem
ory region is no longer valid, and further access attempts result in a
SIGSEGV
signal.
Normally,
munmap()
is passed the return value and the
len
parameter from a previous invocation of
mmap()
.
On success,
munmap()
returns
0
; on failure, it returns
-1
, and
errno
is set appropriately. The only standard
errno
value is
EINVAL
, 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.