1.java mmap
2.System v 和Posix
java mmap
java mmapæ¯ä»ä¹ï¼è®©æ们ä¸èµ·äºè§£ä¸ä¸ï¼
mmapæ¯å°ä¸ä¸ªæ件æè å ¶å®å¯¹è±¡æ å°è¿å åï¼æ件被æ å°å°å¤ä¸ªé¡µä¸ï¼å¦ææ件ç大å°ä¸æ¯ææ页ç大å°ä¹åï¼æåä¸ä¸ªé¡µä¸è¢«ä½¿ç¨ç空é´å°ä¼æ¸ é¶ãmmapå¨ç¨æ·ç©ºé´æ å°è°ç¨ç³»ç»ä¸ä½ç¨å¾å¤§ã
ç®åJavaæä¾çmmapåªæå åæ件æ å°ï¼å ¶ä»IOæä½è¿æ²¡æå åæ å°åè½ã
Javaå åæ å°æ件ï¼Memory Mapped Filesï¼å°±å·²ç»å¨java.nioå ä¸ï¼ä½å®å¯¹å¾å¤ç¨åºå¼åè æ¥è¯´ä»ç¶æ¯ä¸ä¸ªç¸å½æ°çæ¦å¿µãå¼å ¥NIOåï¼Java IOå·²ç»ç¸å½å¿«ï¼èä¸å åæ å°æ件æä¾äºJavaæå¯è½è¾¾å°çæå¿«IOæä½ï¼è¿ä¹æ¯ä¸ºä»ä¹é£äºé«æ§è½Javaåºç¨åºè¯¥ä½¿ç¨å åæ å°æ件æ¥æä¹ åæ°æ®ã
mmapå¨Javaä¸çç¨éæ¯ä»ä¹ï¼
1ã对æ®éæ件使ç¨mmapæä¾å åæ å°I/Oï¼ä»¥é¿å ç³»ç»è°ç¨(readãwriteãlseek)带æ¥çæ§è½å¼éãåæ¶åå°äºæ°æ®å¨å æ ¸ç¼å²åºåè¿ç¨å°å空é´çæ·è´æ¬¡æ°ã
2ã使ç¨ç¹æ®æ件æä¾å¿åå åæ å°ã
3ã使ç¨shm_open以æä¾æ 亲ç¼å ³ç³»è¿ç¨é´çPosixå ±äº«å ååºã
mmapå¨Javaä¸æ¯å¦ä½ä½¿ç¨çï¼ï¼å ·ä½åèkafkaæºç ä¸çOffsetIndexè¿ä¸ªç±»ï¼
æä½æ件ï¼å°±ç¸å½äºæä½ä¸ä¸ªByteBufferä¸æ ·ã public class TestMmap { undefined public static String path = "C:\\Users\\\\Desktop\\mmap"; public static void main(String[] args) throws IOException { undefined File file1 = new File(path, "1"); RandomAccessFile randomAccessFile = new RandomAccessFile(file1, "rw"); int len = ; // æ å°ä¸º2kbï¼é£ä¹çæçæ件ä¹æ¯2kb MappedByteBuffer mmap = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, len); System.out.println(mmap.isReadOnly()); System.out.println(mmap.position()); System.out.println(mmap.limit()); // åæ°æ®ä¹åï¼JVM éåºä¹åä¼å¼ºå¶å·æ°ç mmap.put("a".getBytes()); mmap.put("b".getBytes()); mmap.put("c".getBytes()); mmap.put("d".getBytes()); // System.out.println(mmap.position()); // System.out.println(mmap.limit()); // // mmap.force(); // åèOffsetIndex强å¶åæ¶å·²ç»åé çmmapï¼ä¸å¿ çå°ä¸æ¬¡GCï¼ unmap(mmap); // å¨Windowsä¸éè¦æ§è¡unmap(mmap); å¦åæ¥é // Windows won't let us modify the file length while the file is mmapped // java.io.IOException: 请æ±çæä½æ æ³å¨ä½¿ç¨ç¨æ·æ å°åºåæå¼çæ件ä¸æ§è¡ randomAccessFile.setLength(len/2); mmap = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, len/2); // A mapping, once established, is not dependent upon the file channel // that was used to create it. Closing the channel, in particular, has no // effect upon the validity of the mapping. randomAccessFile.close(); mmap.put(, "z".getBytes()[0]); } // copy from FileChannelImpl#unmap(ç§ææ¹æ³) private static void unmap(MappedByteBuffer bb) { undefined Cleaner cl = ((DirectBuffer)bb).cleaner(); if (cl != null) cl.clean(); } }
System v 和Posix
在Linux系统中进行进程间通信时,我们常常会遇到System v和POSIX两种类型的接口。POSIX,全称Portable Operating System Interface for Computing Systems,支持文档预览的网盘源码是由IEEE和ISO/IEC合作制定的一套标准,旨在确保应用程序在不同操作系统上源代码级别的移植性。它源于年代早期的UNIX用户组工作,旨在统一System V(由AT&T开发)和Berkeley BSD系统之间的调用接口,最终在年发布了POSIX.1标准(IEEE .1-)。
System V,最初称为AT&T System V,nodejs实战 源码是Unix众多版本中的一员,由AT&T开发于年,历经多个版本,其中SVR4(System V Release 4)最为成功,影响了诸如初始化脚本(/etc/init.d)等UNIX共同特性。maven 源码下载System V不仅仅局限于AT&T的硬件,许多转售版本如Dell SVR4和Bull SVR4也基于其。如今,SCO OpenServer基于System V Release 3,而SUN Solaris和SCO UnixWare则基于SVR4。tvb gotv源码System V被视为UNIX两种主要风格(另一是BSD)之一,但随着Linux和QNX等非传统实现的出现,这一分类不再准确。标准化努力如POSIX致力于减少不同实现之间的差异。
System V和POSIX在Linux/unix系统编程中扮演着接口协议的qt 布局源码角色,POSIX相对System V更新,语法简洁。进程间通信(IPC)是两者共同关注的领域,POSIX IPC中对象有名称,如mq_open、sem_open、shm_open中的名称并不一定对应文件系统。创建或打开IPC对象时,需要指定操作模式,如读写权限。而System V IPC中,key_t类型在msget、semget、shmget等函数中扮演关键角色。
2024-12-29 00:43
2024-12-29 00:22
2024-12-28 23:56
2024-12-28 23:49
2024-12-28 22:34
2024-12-28 22:31
2024-12-28 22:20
2024-12-28 22:02