Linux在mmap虚拟内存并不分配物理页,读写时才分配物理页

#include<sys/mman.h> //mmap mincore
#include<stdio.h>
#include <sys/sysinfo.h>
#include <unistd.h> //sysconf
#include<sys/types.h>
#include<stdint.h>
#include<stdlib.h>

void *mem;
unsigned char vec;
long pagesize;
void check_paged(int i){

}
int main(){
    pagesize = sysconf(_SC_PAGESIZE);//get system page size, normally it is 4kb.
    printf("mmap 16 pages and its header pointer is mem\n");
    //malloc 16 pages on heap
    mem = mmap(NULL, 16*pagesize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    //before writing something on these 16 pags, check whether they are paged in physical memory or not.
    printf("Before Write mem[i] a byte, checked whether mem[i] is paged or not\n");
    for( int i=0; i<16; ++i){
            if(-1 == mincore(mem+i*pagesize, pagesize, &vec))
            perror("mincore");
        else{
            if(vec & 0x01)
                printf("mem[%d page] is paged in physical memory\n",i);
            else
                printf("mem[%d page] is not paged in physical memory\n",i);
        } 
    }
    //after writing something on these 16 pags, check whether they are paged in physical memory or not.
    printf("After Write mem[i] a byte, checked whether mem[i] is paged or not\n");
    for( int i=0; i<16; ++i){
        ((uint8_t *)mem)[i * pagesize] = i;
        if(-1 == mincore(mem+i*pagesize, pagesize, &vec))
            perror("mincore");
        else{
            if(vec & 0x01)
                printf("mem[%d page] is paged in physical memory\n",i);
            else
                printf("mem[%d page] is not paged in physical memory\n",i);
        } 
    }
    return 0;
}

源代码保存为 paging.c

编译链接 gcc -O0 -Wall paging.c -o paging.elf

运行 ./paging.elf

xyz@xyz:/media/xyz/disk_d/Linux/LinuxC/chapter2025/paging$ make
clang -Wall -O3 paging.c -c -o paging.o
clang paging.o -o paging.elf
xyz@xyz:/media/xyz/disk_d/Linux/LinuxC/chapter2025/paging$ ./paging.elf 
mmap 16 pages and its header pointer is mem
Before Write mem[i] a byte, checked whether mem[i] is paged or not
 mem[0 page] is not paged in physical memory
 mem[1 page] is not paged in physical memory
 mem[2 page] is not paged in physical memory
 mem[3 page] is not paged in physical memory
 mem[4 page] is not paged in physical memory
 mem[5 page] is not paged in physical memory
 mem[6 page] is not paged in physical memory
 mem[7 page] is not paged in physical memory
 mem[8 page] is not paged in physical memory
 mem[9 page] is not paged in physical memory
 mem[10 page] is not paged in physical memory
 mem[11 page] is not paged in physical memory
 mem[12 page] is not paged in physical memory
 mem[13 page] is not paged in physical memory
 mem[14 page] is not paged in physical memory
 mem[15 page] is not paged in physical memory
After Write mem[i] a byte, checked whether mem[i] is paged or not
 mem[0 page] is paged in physical memory
 mem[1 page] is paged in physical memory
 mem[2 page] is paged in physical memory
 mem[3 page] is paged in physical memory
 mem[4 page] is paged in physical memory
 mem[5 page] is paged in physical memory
 mem[6 page] is paged in physical memory
 mem[7 page] is paged in physical memory
 mem[8 page] is paged in physical memory
 mem[9 page] is paged in physical memory
 mem[10 page] is paged in physical memory
 mem[11 page] is paged in physical memory
 mem[12 page] is paged in physical memory
 mem[13 page] is paged in physical memory
 mem[14 page] is paged in physical memory
 mem[15 page] is paged in physical memory
xyz@xyz:/media/xyz/disk_d/Linux/LinuxC/chapter2025/paging$ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值