FAQ Database Discussion Community


Is it possible to write to a mmap'd file without getting a bus error

c++,c,mmap
I'm trying to monitor a file that I create for when the contents change from 0 to 1. This code create the file and maps it fine, I then spin, waiting for the contents of *map to change from '0' to '1'. However as soon as I run echo 1...

Using mmap to allocate double 2D array

c,arrays,double,mmap
I am attempting to use mmap to create/access twi different (N+2)*(N+2) 2D array of doubles, so that multiple threads can look at their own portion of it and change it, applying the changes so that all others can see. Here is what I have: int main(int argc, char *argv[]) {...

C - Memory Mapping with Multiple Processes

c,memory,mapping,mmap
I have an assignment requiring me to write a multi-processed program that works with a memory-mapped file containing a string of characters. After the parent process maps the file to memory, it spawns 2 children processes to modify the file. Child 1 outputs the contents of the file, converts the...

linux -c - notify the kernel from userspace as fast as possible and vice versa

c,linux,linux-kernel,mmap,notify
Context : Debian 64 bits. Making a linux-only userspace networking stack that I may release open source. Everything is ready but one last thing. The problem : I know about poll/select/epoll and use them heavily already but they are too complicated for my need and tend to add latency (few...

In the following case, which one is better ? fread() or mmap()?

c,linux,memory,mmap,fread
I want to read two files via a process, the first file is about 2G, the second file is about 20M. They look like this: 1 1217907 1 1217908 1 1517737 1 2 1 3 1 4 1 5 Now I plan to read the whole file into memory, then...

Lock mutex in shared memory

c,mutex,mmap
I am trying to use pthread_mutex_t in a mutil Multi-process program, and I need to lock a mutex in a shared memory for synchronization. Here is the code I copy from. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/mman.h> #include <pthread.h> pthread_mutex_t* g_mutex; void init_mutex(void) { int ret;...

Creating array with mmap function. Assembler x86_64 AT&T

linux,assembly,x86-64,mmap,syscall
I want to create an array that will contains for example 4 values Here's my code: mov $32,%rsi # 4 x 8bytes mov $9,%rax mov $0,%rdi mov $0x3,%rdx mov $0x01,%r10 mov $0,%r9 syscall Now I am having a new adress of alocated 32 bytes in rax? When I am trying...

Driver mmap operation Page table creation

linux-kernel,linux-device-driver,mmap,virtual-memory,page-tables
I am trying to understand mmap operation in a driver. I know why we are doing mmap. I am confused with the following 2 implementations of mmap: int my_mmap(struct file *filp, struct vm_area_struct *vma) { vma->vm_ops = &mmap_vm_ops; vma->vm_flags |= VM_RESERVED; /* assign the file private data to the vm...

How to mmap a struct as shared anonymous memory?

c,struct,mmap
I am trying to mmap a struct as anonymous shared memory so it can be shared across multiple child processes. However whenever I access the struct outside of the functions that create it, I get a segfault. The code is derived from trinity fuzzer, which can be found here https://github.com/kernelslacker/trinity...

get N-th byte from mmaped file

c,memory,segmentation-fault,mmap,coredump
I'm new at C and now I'm learning about mmap. I want to get N-th byte from mmaped file, but I get this error Segmentation Fault (core dumped) When I test my program with gdb I get that something is wrong with this line printf("%d\n", (int) data[sk]); and then I...

mmap file with one additional page that is not backed by the file

c++,c,linux,mmap
I need to mmap a file in read-only mode but need an additional page at the end of the mapping that is \0-ed out. My initial idea was to mmap an additional page and claim it by writing to it. int file=::open(name,O_RDONLY); size_t size=lseek(file,0,SEEK_END); size_t pageSize=::sysconf( _SC_PAGESIZE); int padding=size%pageSize; size_t...

memory mapping files for high frequency trading?

ipc,mmap,quickfix,fix,memory-mapping
I am working in a high frequency trading client for stocks in C++. I am using the quickfix/J library to receive information from my broker. I need these two processes to communicate (one written in Java and the other in C++) and the latency has to be minimized as much...

malloc() - Does it use brk() or mmap()

c,memory-management,malloc,mmap,sbrk
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...

Implementing copy-on-write buffer with mmap on Mac OS X

c,osx,mmap,copy-on-write
I've been playing around with copy-on-write buffers on Linux and the following example seems to work as intended: #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #define SIZE 4096 #define SHM_NAME "foobar" int main(void) { int fd = shm_open(SHM_NAME, O_RDWR | O_CREAT,...

How to create and write to memory mapped files?

rust,mmap
I'm trying to create a memory mapped file using std::os::MemoryMap. The current approach looks as follows: use std::os; use std::ptr; use std::old_io as io; use std::os::unix::prelude::AsRawFd; use std::os::MapOption; let path = Path::new("test.mmap"); let f = match io::File::open_mode(&path, io::Open, io::ReadWrite) { Ok(f) => f, Err(err) => panic!("Could not open file: {}",...

mmap() and pthreads - copy VMA

c,linux-kernel,pthreads,mmap
I mmap() an anonymous VMA. How do pthreads handle that VMA? I want that the vma with its memory is copied for every thread. Which flags do I need for that purpose?

Using mmap and madvise for huge pages

c,linux,memory-management,mmap,huge-pages
I want to allocate memory on the hugepages being used by a Linux machine. I see that there are two ways to do this, using mmap and madvise. That is, using the MAP_HUGETLB flag with the mmap call - base_ptr_ = mmap(NULL, memory_size_, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS |...

Slow memory allocation in OSX

c,osx,mmap
I'm trying to trace down a memory allocation problem I have in OSX. If I compile and run the following code normally, it will run pretty fast. #include <sys/mman.h> #define SIZE 8 * 1024 * 1024 int main(int argc, char const *argv[]) { for (int i = 0; i <...

Traversing `vm_area_struct`'s of a process

c,linux,linux-kernel,filesystems,mmap
As an extension of an answer to Traversing all the physical pages of a process and data here http://www.makelinux.net/ldd3/chp-15-sect-1, I had a small question. In the first answer as suggested to traverse the physical pages of a process, struct vm_area_struct *vma = 0; unsigned long vpage; if (task->mm && task->mm->mmap)...

Multiple mappings for a physical page

c,linux,mmap,virtual-memory
I want to create a copy-on-write like interface for accessing a mmap()ed file in GNU C. Here is the way it should work: I will map the file to the address space using mmap(). Doing so, I will have a pointer to a contiguous region of memory which will contain...

MMAP, “TypeError: 'str' does not support the buffer interface” | Python

python-3.x,mmap
f = open('C:\Python33\File.doc') s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) if (s.find("blabla")) != -1: print("True") When I run the following code, I get the error "TypeError: 'str' does not support the buffer interface", why is this, and what can I do to solve this problem? I have searched online for the answer,...

How to use munmap custom malloc

c++,c,malloc,mmap
I'm trying to recode malloc and free functions but i got a problem with munmap.. With my custom malloc, I create a pool of memory with mmap (Ex : 4Kb), so I can return an index of this pool as an address when my malloc is called. This work fine,...

Asm x86 segmentation fault in reading from file

assembly,x86,segmentation-fault,mmap
I'm receiving segmentation fault in 5th line of loop2. This is the part of my code which is reading values from mmapped file byte by byte. Could you please tell me what I'm doing wrong? .global _start _start: mov $2,%rax mov 16(%rsp),%rdi mov $02,%rsi syscall cmp $0,%rax jl exit 1:...

mmap2 function write in asm, call in c

c,assembly,mmap
I have problem with writing MMAP2 in ASM AT&T and call it in C. I wrote this but didn't know how should it works. I am knowingly that code is not good but I very need help. Can you tell me how should it looks ? Thanks for help! .data...