#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$