mmap

Analyzing memory mapping of a process with pmap. [stack]

时光怂恿深爱的人放手 提交于 2019-11-29 15:34:02
I'm trying to understand how stack works in Linux. I read AMD64 ABI sections about stack and process initialization and it is not clear how the stack should be mapped. Here is the relevant quote (3.4.1): Stack State This section describes the machine state that exec (BA_OS) creates for new processes. and It is unspecified whether the data and stack segments are initially mapped with execute permissions or not. Applications which need to execute code on the stack or data segments should take proper precautions, e.g., by calling mprotect() . So I can deduce from the quotes above that the stack

Avoid crash after doing mmap() on a file that is unmounted

[亡魂溺海] 提交于 2019-11-29 14:58:41
问题 I'm doing mmap() on a file that can be unmounted ( the file is located on an USB device which the user can remove at any time ), and my application crashes if the file is unmounted and then i try to access any element in the buffer. Any solutions to this ? 回答1: First of all, I would like to say this should serve as a good argument not to use mmap unnecessarily as an "optimized read" or similar. Aside from device removal, issues like file truncation by other processes can cause accesses to

Why is MAP_GROWSDOWN mapping does not grow?

穿精又带淫゛_ 提交于 2019-11-29 14:39:39
I tried to create MAP_GROWSDOWN mapping with the expectation it would grow automatically. As specified in the manual page: MAP_GROWSDOWN This flag is used for stacks. It indicates to the kernel virtual memory system that the mapping should extend downward in memory. The return address is one page lower than the memory area that is actually created in the process's virtual address space. Touching an address in the "guard" page below the mapping will cause the mapping to grow by a page . This growth can be repeated until the mapping grows to within a page of the high end of the next lower

How to know whether a copy-on-write page is an actual copy?

扶醉桌前 提交于 2019-11-29 14:33:59
问题 When I create a copy-on-write mapping (a MAP_PRIVATE) using mmap, then some pages of this mapping will be copied as soon as I write to specific addresses. At a certain point in my program I would like to figure out which pages have actually been copied. There is a call, called 'mincore', but that only reports whether the page is in memory or not, which is not the same as the page being copied or not. Is there some way to figure out which pages have been copied ? 回答1: Good, following the

Does malloc() use brk() or mmap()?

久未见 提交于 2019-11-29 11:40:09
问题 c code: // program break mechanism // TLPI exercise 7-1 #include <stdio.h> #include <stdlib.h> void program_break_test() { printf("%10p\n", sbrk(0)); char *bl = malloc(1024 * 1024); printf("%x\n", sbrk(0)); free(bl); printf("%x\n", sbrk(0)); } int main(int argc, char **argv) { program_break_test(); return 0; } When compiling following code: printf("%10p\n", sbrk(0)); I get warning tip: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int’ Question 1: Why is that? And

How does numpy handle mmap's over npz files?

我的未来我决定 提交于 2019-11-29 10:45:32
I have a case where I would like to open a compressed numpy file using mmap mode, but can't seem to find any documentation about how it will work under the covers. For example, will it decompress the archive in memory and then mmap it? Will it decompress on the fly? The documentation is absent for that configuration. The short answer, based on looking at the code, is that archiving and compression, whether using np.savez or gzip , is not compatible with accessing files in mmap_mode . It's not just a matter of how it is done, but whether it can be done at all. Relevant bits in the np.load

How to write mmap input memory to O_DIRECT output file?

不想你离开。 提交于 2019-11-29 08:57:30
why doesn't following pseudo-code work (O_DIRECT results in EFAULT) in_fd = open("/dev/mem"); in_mmap = mmap(in_fd); out_fd = open("/tmp/file", O_DIRECT); write(out_fd, in_mmap, PAGE_SIZE); while following does (no O_DIRECT) in_fd = open("/dev/mem"); in_mmap = mmap(in_fd); out_fd = open("/tmp/file"); write(out_fd, in_mmap, PAGE_SIZE); I guess it's something with virtual kernel pages to virtual user pages, which cannot be translated in the write call? Best regards, Friedrich Using mmap() with O_DIRECT is tricky. There are some restrictions. The output to the file should be block aligned. For

Fast resize of a mmap file

十年热恋 提交于 2019-11-29 08:56:04
问题 I need a copy-free re-size of a very large mmap file while still allowing concurrent access to reader threads. The simple way is to use two MAP_SHARED mappings (grow the file, then create a second mapping that includes the grown region) in the same process over the same file and then unmap the old mapping once all readers that could access it are finished. However, I am curious if the scheme below could work, and if so, is there any advantage to it. mmap a file with MAP_PRIVATE do read-only

Does madvise(___, ___, MADV_DONTNEED) instruct the OS to lazily write to disk?

不羁岁月 提交于 2019-11-29 07:02:47
Hypothetically, suppose I want to perform sequential writing to a potentially very large file. If I mmap() a gigantic region and madvise(MADV_SEQUENTIAL) on that entire region, then I can write to the memory in a relatively efficient manner. This I have gotten to work just fine. Now, in order to free up various OS resources as I am writing, I occasionally perform a munmap() on small chunks of memory that have already been written to. My concern is that munmap() and msync()will block my thread, waiting for the data to be physically committed to disk. I cannot slow down my writer at all, so I

mmap与普通文件读写

时间秒杀一切 提交于 2019-11-29 06:18:45
常规文件系统操作(调用read/fread等类函数)中,函数的调用过程: 1、进程发起读文件请求。 2、内核通过查找进程文件符表,定位到内核已打开文件集上的文件信息,从而找到此文件的inode。 3、inode在address_space上查找要请求的文件页是否已经缓存在页缓存中。如果存在,则直接返回这片文件页的内容。 4、如果不存在,则通过inode定位到文件磁盘地址,将数据从磁盘复制到页缓存。之后再次发起读页面过程,进而将页缓存中的数据发给用户进程。 总结来说,常规文件操作为了提高读写效率和保护磁盘,使用了页缓存机制。这样造成读文件时需要先将文件页从磁盘拷贝到页缓存中,由于页缓存处在内核空间,不能被用户进程直接寻址,所以还需要将页缓存中数据页再次拷贝到内存对应的用户空间中。这样,通过了两次数据拷贝过程,才能完成进程对文件内容的获取任务。写操作也是一样,待写入的buffer在内核空间不能直接访问,必须要先拷贝至内核空间对应的主存,再写回磁盘中(延迟写回),也是需要两次数据拷贝。 而使用mmap操作文件中,创建新的虚拟内存区域和建立文件磁盘地址和虚拟内存区域映射这两步,没有任何文件拷贝操作。而之后访问数据时发现内存中并无数据而发起的缺页异常过程,可以通过已经建立好的映射关系,只使用一次数据拷贝,就从磁盘中将数据传入内存的用户空间中,供进程使用。 总而言之