Why does munmap needs a length as parameter?

一笑奈何 提交于 2019-12-05 00:15:51

One can map, say, 5 pages and later unmap one of them. And information about what pages to unmap is passed as address and length where the length is a multiple of page size.

You can munmap a subrange of memory addresses that you have previously mapped. For example:

#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>

int main()
{
    int pagesize = sysconf(_SC_PAGESIZE);
    char *addr = mmap(NULL, 4 * pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    addr[pagesize] = 'X';
    munmap(addr, pagesize);

    printf("%c\n", addr[pagesize]);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!