mmap---关于读写文件

mmap函数是unix/linux下的系统调用,来看《Unix Netword programming》卷二12.2节有详细介绍。

mmap系统调用并不是完全为了用于共享内存而设计的。它本身提供了不同于一般对普通文件的访问方式,进程可以像读写内存一样对普通文件的操作。而Posix或系统V的共享内存IPC则纯粹用于共享目的,当然mmap()实现共享内存也是其主要应用之一。
          mmap系统调用使得进程之间通过映射同一个普通文件实现共享内存。普通文件被映射到进程地址空间后,进程可以像访问普通内存一样对文件进行访问,不必再调用read(),write()等操作。mmap并不分配空间, 只是将文件映射到调用进程的地址空间里, 然后你就可以用memcpy等操作写文件, 而不用write()了.写完后用msync()同步一下, 你所写的内容就保存到文件里了. 不过这种方式没办法增加文件的长度, 因为要映射的长度在调用mmap()的时候就决定了.

简单说就是把一个文件的内容在内存里面做一个映像,内存比磁盘快些。
基本上它是把一个档案对应到你的virtual memory 中的一段,并传回一个指针。

以后对这段 memory 做存取时,其实就是对那个档做存取。
它就是一种快速 file I/O 的东东,而且使用上和存取 memory 一样方便,只不过会占掉你的 virutal memory。
#include <sys/types.h>
#include <sys/stat.h> //文件状态结构
#include <unistd.h>
#include <sys/mman.h> //mmap头文件

void * mmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);

mmap开启记忆体对映。 
start指定记忆体位置,通常都是用NULL。offset指定档案要在那里开始对映,通常都是用0。 

int munmap(void *start, size_t length);

int msync(const void *start, size_t length, int flags); 
如果开启记忆体对映是希望写入档案中,那麽修改过的记忆体会在一段时间内与档案稍稍有点不同。如果您希望立即将资料写入档案中,可使用msync。 

start为记忆体开始位置,length为长度。 

flags则有三个: 
MS_ASYNC : 请Kernel快将资料写入。 
MS_SYNC : 在msync结束返回前,将资料写入。 
MS_INVALIDATE : 让核心自行决定是否写入,仅在特殊状况下使用

例子:
if( (fp = open("./data.bin",O_RDONLY) ) < 0 )
{
cout<<" Can not open !"<<endl;
exit(0);
}
if( (fstat(fp,&stat_data) ) < 0 )
{
cout<<" fstat error !";
exit(0);
}
if( ( start_fp = mmap(NULL,stat_data.st_size,
PROT_READ,MAP_SHARED,fd_denseindex,0 )) == (void *)-1)
{
cout<<" mmap error !"<<endl;
exit(0);
}
这样便能从start_fp开始读取数据啦!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中集成mmap读写文件可以通过以下步骤实现: 1. 引入依赖:在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> ``` 2. 创建文件操作工具类:创建一个`FileUtils`工具类,用于进行文件读写操作。在该类中,可以使用Java的`MappedByteBuffer`类来实现mmap读写文件的功能。以下是一个简单的示例代码: ```java import java.io.File; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class FileUtils { public static void writeFileWithMmap(String filePath, String content) throws Exception { File file = new File(filePath); RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); FileChannel fileChannel = randomAccessFile.getChannel(); MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, content.length()); mappedByteBuffer.put(content.getBytes()); fileChannel.close(); randomAccessFile.close(); } public static String readFileWithMmap(String filePath) throws Exception { File file = new File(filePath); RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); FileChannel fileChannel = randomAccessFile.getChannel(); MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); byte[] bytes = new byte[(int) file.length()]; mappedByteBuffer.get(bytes); fileChannel.close(); randomAccessFile.close(); return new String(bytes); } } ``` 3. 在Spring Boot应用中使用mmap读写文件:在需要使用mmap读写文件的地方,可以直接调用`FileUtils`工具类中的方法进行文件读写操作。以下是一个简单的示例代码: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); String filePath = "path/to/file.txt"; String content = "Hello, mmap!"; // 文件 FileUtils.writeFileWithMmap(filePath, content); // 读文件 String fileContent = FileUtils.readFileWithMmap(filePath); System.out.println(fileContent); } } ``` 请注意,以上示例代码仅为演示目的,实际使用时需要根据具体需求进行适当的修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值