虚拟内存分配
#include <iostream>
#include <cstdio>
//#include <cmalloc>
using namespace std;
const int block_size=1024*1024*10;
char* address[1024];
int main(){
int total_heap=0;
int i=0;
printf("main function address:%p\n", main);
printf("printf function address:%p\n", printf);
printf("static mem address:%p\n",address);
printf("press enter to alloc heap\n");
while(1){
getchar();
address[i]=new char[block_size];
printf("alloc heap %p, total: %d M\n",address[i],i);
printf("%d\n",*((int*)address[i]-1));
printf("%d\n",*((int*)address[i]-2));
i++;
}
return 0;
}
这里可以把输出和cat /proc/{pid}/maps
的输出中的地址做对比
>>> 1052674-1024*1024
4098
main function address:0x4007ed
printf function address:0x400670
static mem address:0x6010a0
press enter to alloc heap
alloc heap 0x7f0a08945010, total: 1 M
0
1052674
alloc heap 0x7f0a07b49010, total: 2 M
0
1052674
alloc heap 0x7f0a07a48010, total: 3 M
0
1052674
还有就是每块新分配的地址前面的这个表示长度的东西总是比代码中定义的block_size 多4098,不知道为啥?
刚好是一个块的大小,存储malloc的地址的结构体的其他部分
mmap
// use mmap
#include <iostream>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include "file_data.h"
using namespace std;
static char buff[1024];
int main(){
int fd=open(data_file, O_RDWR,0666);
if(fd<0){
perror("can not open file");
exit(-1);
}
char *p=(char*)mmap(NULL,map_size, PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
// for(int i=0;i<map_size;i++){
// p[i]=2;
// }
for(int i=0;i<map_size/1024;i++){
memcpy(p+1024*i,buff,1024);
}
int rc=munmap(p,file_size);
if(rc<0){
perror("munmap");
}
close(fd);
return 0;
}
// use write
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include "file_data.h"
#include "util.h"
#include <cassert>
using namespace std;
char buff[1024];
int main(){
int fd=open(data_file,O_WRONLY);
check_rc(fd,"can not open file");
lseek(fd, 0, SEEK_SET);
int total=0;
while(total< map_size) {
int l=write(fd, buff, 1024);
check_rc(l,"write");
total+=l;
}
return 0;
}
从下面的输出看,mmap 的耗时约为 write 的1/4
$ time ./file_mmap
real 0m0.554s
user 0m0.314s
sys 0m0.234s
$ time ./file_write
real 0m1.733s
user 0m0.410s
sys 0m1.120s
几个注意地方:
- 匿名映射 fd 传 -1
- 如果mmap 中的那个映射长度是可以大于/小于文件长度的,但是实际写入时,超过mmap定义的size 或者 超过文件的长度都会报错
- open 时,文件要有读写的权限,只有写权限会报错,不知道为啥,按理说应该权限匹配就可以,但是open 和 mmap 都改成只有写权限会报错
- mmap 后关闭文件,写入依然有效
程序kill 的 文件写入测试
// write file
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fcntl.h>
#include <sys/mman.h>
#include "util.h"
#include "file_data.h"
#include <ctime>
#include <unistd.h>
using namespace std;
int main() {
int fd = open(file_name, O_RDWR);
check_rc(fd,"can not open file");
srand((unsigned int)time(NULL));
char *p = (char*)mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if( (long long)p==-1){
perror("mmap error");
}
for(int i=0;i<file_size;i++){
int r=rand()%256;
p[i]= (char)r;
printf("write to %d:%d\n",i,r);
}
return 0;
}
//read file
#include<iostream>
#include <fcntl.h>
#include <unistd.h>
#include "file_data.h"
using namespace std;
int main(int argc, char ** argv){
unsigned char buff[1024];
int fp=open(file_name,O_RDONLY);
if(fp<0){
perror("can not open file");
exit(-1);
}
int offset;
if(argc==1){
offset=0;
}else{
offset=atoi(argv[1]);
}
lseek(fp,offset,SEEK_SET);
read(fp,buff,1);
close(fp);
unsigned x=buff[0];
printf("read from offset %d:%u\n",offset,(buff[0]));
return 0;
}
参考:
https://www.jianshu.com/p/eece39beee20
https://en.wikipedia.org/wiki/Mmap
https://www.cnblogs.com/huxiao-tee/p/4660352.html (不是很好理解,不过极端情况讲得比较好)