mmap

Mmap system call operation that is able to access memory locations

一笑奈何 提交于 2020-01-11 09:54:06
问题 I am writing a program that allocates huge chunks of memory using mmap and then accesses random memory locations to read and write into it. I just tried out the following code: #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> int main() { int fd,len=1024*1024; fd=open("hello",O_READ); char*addr=mmap(0,len,PROT_READ+PROT_WRITE,MAP_SHARED,fd,0); for(fd=0;fd<len;fd++) putchar(addr[fd]); if (addr==MAP_FAILED) {perror("mmap"); exit(1);} printf("mmap returned %p, which seems readable

vmalloc与mmap

别说谁变了你拦得住时间么 提交于 2020-01-07 11:17:23
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> vmalloc与mmap    mmap()系统调用是在用户进程与内核之间共享内存区域的常用方法。我们最近有个程序,需要应用进程能够读取内核驱动获取的数据,经过简单的调研,决定采用mmap方式。实现起来不难,在驱动中注册一个字符设备,实现该设备的mmap()方法即可。但这其中有一点小曲折。   在实现设备的mmap()方法时,需要将物理内存映射到应用程序通过mmap()系统调用传下来的vma中。vma代表的是进程的一段虚拟地址空间。在第一版里,考虑的不全面,利用alloc_pages()将整个内存段申请为一段连续的物理地址空间。然后通过remap_pfn_range()函数将这段连续的物理内存映射到vma中。经过长时间的测试,没有发现问题。直到今天,在部署一个老集群时,遇到了问题。这个集群中有很多老机器,内存只有十多个G,而且长时间运行后产生了大量的内存碎片。从而导致,我们无法获得足够的连续物理内存。没办法,只好重新调整驱动中分配内存的方式,改用vmalloc获取地址空间。   在kernel里,通常有3种申请内存的方式:vmalloc, kmalloc, alloc_pages。kmalloc与alloc_pages类似,均是申请连续的地址空间。而vmalloc则可以申请一段不连续的物理地址空间

Using read() and write() system calls to access mmapped() memory

妖精的绣舞 提交于 2020-01-06 15:19:07
问题 I wish to ask whether is it possible to implement an mread() function using only system calls such as read() and write(). I know that the standard C library functions such as fread() and fwrite() use a type of buffer to read. I want to do the same but this time I want to read() and write() from a buffer to a memory mapped region and vice versa. For example my mread() wants to read from memory map and store into buffer. Does it make sense to create a new fd just to use it as my TEMPORARY

c++ close a open() file read with mmap

强颜欢笑 提交于 2020-01-04 07:18:10
问题 I am working with mmap() to fastly read big files, basing my script on this question answer (Fast textfile reading in c++). I am using the second version from sehe answer : #include <algorithm> #include <iostream> #include <cstring> // for mmap: #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> const char* map_file(const char* fname, size_t& length); int main() { size_t length; auto f = map_file("test.cpp", length); auto l = f + length; uintmax_t m_numLines = 0; while (f && f!=l)

C - why I cannot mmap a small (256UL or smaller) size of memory?

谁说胖子不能爱 提交于 2020-01-03 12:34:15
问题 Please tell me, why my simple application cannot mmap a small size of memory? And, why such a specific boundary - 257UL? // #define MAP_SIZE 256UL or below - fail // #define MAP_SIZE 257UL - ok #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h> #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ _

How to memory map (mmap) a linux block device (e.g. /dev/sdb) in Java?

爷,独闯天下 提交于 2020-01-01 09:36:09
问题 I can read/write a linux block device with Java using java.nio . The following code works: Path fp = FileSystems.getDefault().getPath("/dev", "sdb"); FileChannel fc = null; try { fc = FileChannel.open(fp, EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE)); } catch (Exception e) { System.out.println("Error opening file: " + e.getMessage()); } ByteBuffer buf = ByteBuffer.allocate(50); try { if(fc != null) fc.write(buf); } catch (Exception e) { System.out.println("Error writing to

Is mmap deterministic if ASLR is disabled?

▼魔方 西西 提交于 2020-01-01 09:32:33
问题 If Address Space Layout Randomization (ASLR) is disabled, would we have a deterministic mmap ? By deterministic, I mean that If I run the same application again and again with the same inputs, will I get the same addresses returned by mmap ? I am mostly interested in anonymous mmaps. 回答1: If Address Space Layout Randomization (ASLR) is disabled, would we have a deterministic mmap? If your application has exactly the same memory layout at moment of i-th mmap (in terms of which pages of virtual

Poor memcpy performance in user space for mmap'ed physical memory in Linux

早过忘川 提交于 2020-01-01 08:54:03
问题 Of 192GB RAM installed on my computer, I have 188GB RAM above 4GB (at hardware address 0x100000000) reserved by the Linux kernel at boot time (mem=4G memmap=188G$4G). A data acquisition kernel modules accumulates data into this large area used as a ring buffer using DMA. A user space application mmap's this ring buffer into user space, then copies blocks from the ring buffer at the current location for processing once they are ready. Copying these 16MB blocks from the mmap'ed area using

Poor memcpy performance in user space for mmap'ed physical memory in Linux

瘦欲@ 提交于 2020-01-01 08:53:10
问题 Of 192GB RAM installed on my computer, I have 188GB RAM above 4GB (at hardware address 0x100000000) reserved by the Linux kernel at boot time (mem=4G memmap=188G$4G). A data acquisition kernel modules accumulates data into this large area used as a ring buffer using DMA. A user space application mmap's this ring buffer into user space, then copies blocks from the ring buffer at the current location for processing once they are ready. Copying these 16MB blocks from the mmap'ed area using

MAP_ANONYMOUS with C99 standard

孤街醉人 提交于 2020-01-01 07:33:08
问题 I have an application that uses the mmap system call, I was having an issue getting it to compile for hours looking as to why I was getting MAP_ANON and MAP_ANONYMOUS were undeclared, I had a smaller section of code that I used and I saw I could compile it just fine so I tried just a basic compile and that worked, I saw that it fails when you add -std=c99. Is there a specific reason that MAP_ANON and MAP_ANONYMOUS are not valid in the C99 standard? I know that they aren't defined by POSIX but