标签:att allocator sprintf UNC 一点 link int led style
内存映射文件的方法
-----------------------------------------------------
Windows采用MapViewOfFile系统api,Linux则采用mmap相关函数。
之前在做大数据查询计算的时候,经常会出现内存不足的情况,malloc无法分配内存了。
那时候就经常在想能不能使用硬盘来充当内存,就像swap交换那样,硬盘是非常大的,如
果能使用硬盘来当内存也许速度会慢一点,但是内存问题就不需要再担心了。之后研究了
一段时间发现,内存映射文件依然是将整个文件映射到内存里,所以:
内存映射文件是无法利用文件来节省内存的!!
-----------------------------------------------------
Windows Demo:
#include <stdio.h>
#include <windows.h>
#define MMAP_ALLOCATOR_SIZE 1024
int main (int argc, char *argv[]) {
HANDLE dumpFileDescriptor = CreateFile("1.txt", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
printf("dumpFileDescriptor=%d\n", dumpFileDescriptor);
HANDLE fileMappingObject = CreateFileMapping(dumpFileDescriptor,
NULL,
PAGE_READWRITE,
0,
MMAP_ALLOCATOR_SIZE,
NULL);
printf("fileMappingObject=%d\n", fileMappingObject);
char* mappedFileAddress = (char *)MapViewOfFile(fileMappingObject,
FILE_MAP_ALL_ACCESS,
0,
0,
MMAP_ALLOCATOR_SIZE);
printf("mappedFileAddress=%d\n", mappedFileAddress);
memset(mappedFileAddress, 0, MMAP_ALLOCATOR_SIZE);
sprintf(mappedFileAddress, "10086");
printf("mappedFileAddress=%s\n", mappedFileAddress);
FlushViewOfFile(mappedFileAddress, MMAP_ALLOCATOR_SIZE);
UnmapViewOfFile(mappedFileAddress);
CloseHandle(fileMappingObject);
CloseHandle(dumpFileDescriptor);
unlink("1.txt");
return 1;
}
------------------------------------------------------------------
Linux Demo:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <unistd.h>
#define MMAP_ALLOCATOR_SIZE 65535
int main(int argc, char *argv[])
{
struct stat statbuf;
int fd;
char *buf;
int i;
fd = open("1.txt", O_CREAT|O_RDWR, 0755);
if (fd == -1) {
printf("fail to open\n");
exit(1);
}
ftruncate(fd, MMAP_ALLOCATOR_SIZE);
buf = (char *)mmap(NULL, MMAP_ALLOCATOR_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if (buf == MAP_FAILED) {
printf("fail to open\n");
exit(1);
}
memset(buf, 0, MMAP_ALLOCATOR_SIZE);
sprintf(buf, "10086");
msync(buf, MMAP_ALLOCATOR_SIZE, MS_SYNC);
printf("buf=%s\n", buf);
if (munmap(buf, MMAP_ALLOCATOR_SIZE) == -1) {
printf("fail to munmap\n");
exit(1);
}
close(fd);
return 1;
}
相关连接:
https://blog.csdn.net/qq_33611327/article/details/81738195
标签:att allocator sprintf UNC 一点 link int led style
原文地址:https://www.cnblogs.com/hatsusakana/p/12730185.html