mmap

Why does unaligned access to mmap'ed memory sometimes segfault on AMD64?

允我心安 提交于 2019-11-26 16:42:31
I have this piece of code which segfaults when run on Ubuntu 14.04 on an AMD64 compatible CPU: #include <inttypes.h> #include <stdlib.h> #include <sys/mman.h> int main() { uint32_t sum = 0; uint8_t *buffer = mmap(NULL, 1<<18, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); uint16_t *p = (buffer + 1); int i; for (i=0;i<14;++i) { //printf("%d\n", i); sum += p[i]; } return sum; } This only segfaults if the memory is allocated using mmap . If I use malloc , a buffer on the stack, or a global variable it does not segfault. If I decrease the number of iterations of the loop to anything less than 14 it

Mmap() an entire large file

空扰寡人 提交于 2019-11-26 10:19:57
问题 I am trying to \"mmap\" a binary file (~ 8Gb) using the following code (test.c). #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #define handle_error(msg) \\ do { perror(msg); exit(EXIT_FAILURE); } while (0) int main(int argc, char *argv[]) { const char *memblock; int fd; struct stat sb; fd = open(argv[1], O_RDONLY); fstat(fd, &sb); printf(\"Size: %lu\\n\", (uint64_t)sb.st_size); memblock = mmap

Linux进程通信之mmap

和自甴很熟 提交于 2019-11-26 10:14:11
mmap() 函数: void *mmap(void* addr,size_t length,int port,int flags,int fd,off_t offset); 返回:成功:返回创建的映射区首地址;失败:MAP_FAILED 宏 参数: addr: 建立映射区的首地址,由linux内核决定。使用时直接传递NULL; length: 欲创建映射区的大小 port: 映射区权限PROT _READ、PROT_WRITE 、PROT _READ|PROTWRITE flags: 标志位参数(常用于设定更新物理区域、设置共享、创建匿名映射区) MAP_SHARED: 会将映射区所做的操作反射到物理设备上 MAP_PRIVATE: 映射区所作的修改不会反映到物理设备。 fd: 用来建立映射区的文件描述符 offset: 映射文件的偏移(4K的整数倍) /*** mmap.c ***/ #include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<string.h> #include<stdlib.h> #include<sys/mman.h> int main() { int len,ret; char *p = NULL; int fd = open("mytest.txt",O_CREAT|O_RDWR

How to access(if possible) kernel space from user space?

大兔子大兔子 提交于 2019-11-26 09:21:47
问题 How exactly is user memory and kernels memory differentiated inside the Linux kernel (in terms of giving security to kernel space)? What are the different ways I can write in kernel address space from user space? One way I know is through a system call . There are multiple system calls we can use, but at the end they are all system calls. Even in system calls, we send a data to kernel space, where it(driver or respective module) calls functions like copy_from_user() to copy data from user

Setting up Laravel on a Mac php artisan migrate error: No such file or directory [duplicate]

荒凉一梦 提交于 2019-11-26 09:18:55
问题 This question already has an answer here: MySQL connection not working: 2002 No such file or directory 20 answers Pulled a perfectly working laravel project from a git into a mac running MAMP. Project ran perfectly on a linux machine. composer install php artisan migrate, got the following error: [PDOException] SQLSTATE[HY000] [2002] No such file or directory NB: php -v is 5.5 and mysql -v is 5.5 from the terminal Here is part of my config/database.php \'mysql\' => array( \'driver\' => \

How big can a memory-mapped file be?

China☆狼群 提交于 2019-11-26 09:09:12
问题 What limits the size of a memory-mapped file? I know it can\'t be bigger than the largest continuous chunk of unallocated address space, and that there should be enough free disk space. But are there other limits? 回答1: You're being too conservative: A memory-mapped file can be larger than the address space. The view of the memory-mapped file is limited by OS memory constraints, but that's only the part of the file you're looking at at one time. (And I guess technically you could map multiple

How to access mmaped /dev/mem without crashing the Linux kernel?

对着背影说爱祢 提交于 2019-11-26 08:25:13
问题 I have a simple program that tries to access the physical memory in user space, where the kernel stores the 1st struct page. On a 64 bit machine this address is: kernel virtual address: ffffea0000000000 physical address: 0000620000000000 I am trying to access this physical address through mmap in user space. But the following code crashes the kernel. int *addr; if ((fd = open(\"/dev/mem\", O_RDWR|O_SYNC)) < 0 ) { printf(\"Error opening file. \\n\"); close(fd); return (-1); } /* mmap. address

Why does unaligned access to mmap&#39;ed memory sometimes segfault on AMD64?

為{幸葍}努か 提交于 2019-11-26 04:55:08
问题 I have this piece of code which segfaults when run on Ubuntu 14.04 on an AMD64 compatible CPU: #include <inttypes.h> #include <stdlib.h> #include <sys/mman.h> int main() { uint32_t sum = 0; uint8_t *buffer = mmap(NULL, 1<<18, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); uint16_t *p = (buffer + 1); int i; for (i=0;i<14;++i) { //printf(\"%d\\n\", i); sum += p[i]; } return sum; } This only segfaults if the memory is allocated using mmap . If I use malloc , a buffer on the stack, or a global

Will malloc implementations return free-ed memory back to the system?

不羁的心 提交于 2019-11-26 00:43:38
问题 I have a long-living application with frequent memory allocation-deallocation. Will any malloc implementation return freed memory back to the system? What is, in this respect, the behavior of: ptmalloc 1, 2 (glibc default) or 3 dlmalloc tcmalloc (google threaded malloc) solaris 10-11 default malloc and mtmalloc FreeBSD 8 default malloc (jemalloc) Hoard malloc? Update If I have an application whose memory consumption can be very different in daytime and nighttime (e.g.), can I force any of

When should I use mmap for file access?

女生的网名这么多〃 提交于 2019-11-26 00:29:36
问题 POSIX environments provide at least two ways of accessing files. There\'s the standard system calls open() , read() , write() , and friends, but there\'s also the option of using mmap() to map the file into virtual memory. When is it preferable to use one over the other? What\'re their individual advantages that merit including two interfaces? 回答1: mmap is great if you have multiple processes accessing data in a read only fashion from the same file, which is common in the kind of server