java pagecache_02_内核中的 pagecache、mmap 作用、java 文件系统 io、nio、内存中缓存区作用...

本节主要学习内核中 pagecache 页缓存、mmap 内存映射、Java 文件系统 BIO、NIO、内存缓存区的作用。

1. pagecache

image-20210122012950756-20210122183802527-20210122183900676.png

本节主要验证 Java 程序写入文件时,pagecache 的使用、io、nio、内存中缓存区的作用。

Java 程序代码:

import org.junit.Test;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

public class OSFileIO {

static byte[] data = "123456789\n".getBytes();

static String path = "/root/testfileio/out.txt";

public static void main(String[] args) throws Exception {

switch (args[0]) {

case "0":

testBasicFileIO();

break;

case "1":

testBufferedFileIO();

break;

case "2":

testRandomAccessFileWrite();

case "3":

// whatByteBuffer();

default:

}

}

//最基本的file写

public static void testBasicFileIO() throws Exception {

File file = new File(path);

FileOutputStream out = new FileOutputStream(file);

while (true) {

Thread.sleep(10);

out.write(data);

}

}

//测试buffer文件IO

// jvm 8kB syscall write(8KBbyte[])

public static void testBufferedFileIO() throws Exception {

File file = new File(path);

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));

while (true) {

Thread.sleep(10);

out.write(data);

}

}

//测试文件NIO

public static void testRandomAccessFileWrite() throws Exception {

RandomAccessFile raf = new RandomAccessFile(path, "rw");

raf.write("hello mashibing\n".getBytes());

raf.write("hello seanzhou\n".getBytes());

System.out.println("write------------");

System.in.read();

raf.seek(4);

raf.write("ooxx".getBytes());

System.out.println("seek---------");

System.in.read();

FileChannel rafchannel = raf.getChannel();

//mmap 堆外 和文件映射的 byte not objtect

MappedByteBuffer map = rafchannel.map(FileChannel.MapMode.READ_WRITE, 0, 4096);

map.put("@@@".getBytes()); //不是系统调用 但是数据会到达 内核的pagecache

//曾经我们是需要out.write() 这样的系统调用,才能让程序的data 进入内核的pagecache

//曾经必须有用户态内核态切换

//mmap的内存映射,依然是内核的pagecache体系所约束的!!!

//换言之,丢数据

//你可以去github上找一些 其他C程序员写的jni扩展库,使用linux内核的Direct IO

//直接IO是忽略linux的pagecache

//是把pagecache 交给了程序自己开辟一个字节数组当作pagecache,动用代码逻辑来维护一致性/dirty。。。一系列复杂问题

System.out.println("map--put--------");

System.in.read();

// map.force(); // flush

raf.seek(0);

ByteBuffer buffe

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值