mmap

Linux系统函数学习

旧城冷巷雨未停 提交于 2019-12-23 00:40:49
背景 为什么突然想要学习linux系统函数呢,日常工作有没用到,那当时是准备晋升高工和跳槽了,正所谓 面试造火箭,工作拧螺丝。工作中螺丝拧久了,就忘了怎么去提升了,毕竟每天就用那点东西,其他都不会想去了解。这里自己整理了一下使用strace命令查到的系统函数的使用,下次再写一篇关于strace命令使用方法 函数使用 void* mmap(void* start,size_t length,int prot,int flags,int fd,off_t offset); 一种内存映射的方法 如果使用 strace ls -lh /www/index.php 这条命令,实际是去追踪ls如何去计算一个文件的大小和获取它的其他信息,这里会看到很多调用 mmap()这个系统函数 本质是 mmap会把 需要打开处理的文件地址映射到进程的地址空间里,也就是内存中,这样进程在处理这个文件的时候,可以直接在内存中处理,然后系统会把内存中的这些处理再写入硬盘中的原始的文件里,也就是 脏页 的回写,这样会快很多。这里有几点注意 1、mmap映射的是一个物理页(物理页是指真实存在的物理磁盘的大小,虽然我们是整个文件保存在计算机中,但实际上计算机在磁盘也会给这个文件数据进行分页,也就是类似这个文件是一本书,虽然书的内容被整个记录了,但是还是会被分页,32位系统一个物理页是4k,64位是8k)

mmap flag MAP_UNINITIALIZED not defined

蹲街弑〆低调 提交于 2019-12-22 18:39:17
问题 mmap() docs mentions flag MAP_UNINITIALIZED, but the flag doesn't seem to be defined. Tried on Centos7, and Xenial, neither distro has the flag defined in sys/mman.h as alleged. Astonishingly, the internet doesn't seem to be aware of this. What's the story? Edit: I understand from the docs that the flag is only honoured on embedded or low-security devices, but that doesn't mean the flag shouldn't be defined... How do you use it in portable code? Google has revealed code where it is defined as

IPC via mmap'ed file: should atomics and/or volatile be used?

怎甘沉沦 提交于 2019-12-22 13:46:57
问题 I use a mmap'ed file to share data between processes. The code is like this: struct Shared { int Data; }; int file = open("file.dat", O_RDWR); Shared* shared = static_cast<Shared*>( mmap(0, sizeof(Shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, file, 0)); shared->Data++; The questions are: Should I use volatile qualifier ( volatile int Data )? Should I use atomic operations on the shared data ( __sync_fetch_and_add(&(shared->Data), 1) )? For future reference: Volatile: Almost

Avoid copying of data between user and kernel space and vice-versa

陌路散爱 提交于 2019-12-22 09:58:08
问题 I am developing a active messaging protocol for parallel computation that replaces TCP/IP. My goal is to decrease the latency of a packet. Since the environment is a LAN, i can replace TCP/IP with simpler protocol to reduce the packet latency. I am not writing any device driver and i am just trying to replace the TCP/IP stack with something simpler. Now I wanted to avoid copying of a packet's data from user space to kernel space and vice-versa . I heard of the mmap(). Is it the best way to do

Segfault while using mmap in C for reading binary files

社会主义新天地 提交于 2019-12-22 08:02:15
问题 I am trying to use mmap in C just to see how it exactly works. Currently I am try to read a binary file byte by byte using mmap. My code is like this: #include <unistd.h> #include <sys/types.h> #include <sys/mman.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> int main(int argc, char *argv[]) { int fd; char *data; for ( int i = 1; i<argc; i++) { if(strcmp(argv[i],"-i")==0) fd = open(argv[i+1],O_RDONLY); } data = mmap(NULL, 4000, PROT_READ, MAP_SHARED, fd, 8000); int i = 0;

Is there a practical limit on the number of memory mapped files in iOS?

丶灬走出姿态 提交于 2019-12-22 07:41:09
问题 I have an application that can potentially have hundreds of memory mapped, i.e., mmap() , files opened at any point in time. I'm looking for some help understanding what, if any, the practical limit is on the number of opened memory mapped files is. I create these mmap files like: void* map = mmap(0, *capacity, PROT_READ | PROT_WRITE, MAP_SHARED, file, 0); 回答1: iOS kernel allocates around 700mb of virtual memory per process. So that will be your limit. The limit you have on RAM will differ as

Is there a practical limit on the number of memory mapped files in iOS?

蓝咒 提交于 2019-12-22 07:41:07
问题 I have an application that can potentially have hundreds of memory mapped, i.e., mmap() , files opened at any point in time. I'm looking for some help understanding what, if any, the practical limit is on the number of opened memory mapped files is. I create these mmap files like: void* map = mmap(0, *capacity, PROT_READ | PROT_WRITE, MAP_SHARED, file, 0); 回答1: iOS kernel allocates around 700mb of virtual memory per process. So that will be your limit. The limit you have on RAM will differ as

Does mremap “initialize” memory on growth?

荒凉一梦 提交于 2019-12-22 07:30:09
问题 If I've mmap () some PRIVATE and ANONYMOUS pages and then extend them with mremap (), does the new space also get initialized to zeroes? I've tried reading the code for mremap ( mm/mremap.c ) in the linux source but it requires some domain-specific knowledge that I don't currently have (vma_### stuff). Not even sure that's the right place to look... But, from what I've gathered I think that mremap ()ed memory would be cleared, is this correct? Allocation is done like this list = mmap(NULL,

loading file in memory using Python

荒凉一梦 提交于 2019-12-22 05:26:12
问题 I try to load a file in memory with this: import mmap with open(path+fileinput+'example.txt', 'rb') as f: fileinput = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ) When I run the code the error: AttributeError: 'module' object has no attribute 'PROT_READ' 回答1: The PROT_READ and PROT_WRITE are Unix-specific. You're likely looking for: mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) The mmap page actually has different entries for Unix/Windows version. 回答2: I recently got the same error

strace简单介绍

南楼画角 提交于 2019-12-22 05:22:39
1、strace是什么 strace是一个非常简单的工具,它可以跟踪系统调用的执行。最简单的方式,它可以从头到尾跟踪binary的执行,然后以一行文本输出系统调用的名字,参数和返回值。可用于诊断、调试和教学的Linux用户空间跟踪器。我们用它来监控用户空间进程和内核的交互,比如系统调用、信号传递、进程状态变更等。strace底层使用内核的ptrace特性来实现其功能。 2、怎么用 1)strace最简单的用法是执行一个指定的命令(过程中,starce会记录和解析命令进程的所有系统调用及这个进程的所有的信号值),在指定命令结束后立即退出 [root@VM_0_11_centos grub2]# strace cat /boot/grub2/grub.cfg execve("/usr/bin/cat", ["cat", "/boot/grub2/grub.cfg"], [/* 23 vars */]) = 0 brk(NULL) = 0x1741000 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f9eb4042000 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory) open