mmap

Why doesn't POSIX mmap return a volatile void*?

爷,独闯天下 提交于 2019-12-06 20:26:28
问题 Mmap returns a void*, but not a volatile void* . If I'm using mmap to map shared memory, then another process could be writing to that memory, which means two subsequent reads from the same memory location can yield different values -- the exact situation volatile is meant for. So why doesn't it return a volatile void*? My best guess is that if you have a process that's exclusively writing to the shared memory segment, it doesn't need to look at the shared memory through volatile pointers

Python: ctypes hashable c_char array replacement without tripping over '\\0' bytes

最后都变了- 提交于 2019-12-06 15:27:40
For illustration purposes, this script creates a file mapfile containing the content of the files, given as arguments, prepended by a binary header with a sha1 checksum, that allows for duplication detection on subsequent runs. What's needed here is a hashable ctypes.c_char replacement, that can hold sha1 checksums with minimum fuzz, but not choking on '\0' bytes. # -*- coding: utf8 -* import io import mmap import ctypes import hashlib import logging from collections import OrderedDict log = logging.getLogger(__file__) def align(size, alignment): """return size aligned to alignment""" excess =

Converting a pointer to a byte slice

随声附和 提交于 2019-12-06 12:44:47
The Mmap() syscall in the x/sys/unix package in Golang returns a []byte type, while the underlying syscall actually returns a pointer. How does it do this? More specifically, in this package by a Golang developer, the VirtualAlloc function simply returns a pointer. How can this be converted to a byte slice, the same way as it's done in the Unix package? Using the unsafe package you can do the same thing golang.org/x/sys/unix does in the Mmap method of its unexported mmapper type: // Slice memory layout var sl = struct { addr uintptr len int cap int }{addr, length, length} // Use unsafe to turn

evaluate variable/function in gdb

谁都会走 提交于 2019-12-06 11:52:06
I have the following code, ... char* seg = mmap(0, ...) printf("seg=%x\n", seg); ... The program prints with seg=b7ffd000 . While in gdb (for the same execution), when using p/x seg , it prints $2 = 0x0 . I am confused here. Isn't it the same var seg ? why are the values different. PS: in mmap , the first argument is the preferably address of mapped memory and the return value is the actual address of mapped memory. Now that you've answered my question in the comments, I can answer! The value that you see as the result of the printf is the real address. You are seeing 0 as the value of seg in

Setting a fmemopen ed file descriptor to be the standard input for a child process

本小妞迷上赌 提交于 2019-12-06 08:59:39
问题 I have an fmemopen file descriptor(pointing to a buffer in the parent) in Linux and I would like to be able to, in C, set this file descriptor as the standard input for a child process(for whom I do not have access to the code) Is this possible? If so how do I do it? I would like to avoid having to write to disk if at all possible. 回答1: This is not possible. Inheriting stdin/out/err is based purely on file descriptors, not stdio FILE streams. Since fmemopen does not create a file descriptor,

How to use mmap to share user-space and kernel threads

随声附和 提交于 2019-12-06 08:52:37
问题 I am having some trouble finding some suitable examples to solve my problem. I want to share 4K (4096) byte of data between user and kernel space. I found many ideas which says I have to allocate memory from kernel and mmap it in user-space. Can someone provide an example on how to do it in Linux 2.6.38. Is there any good document which explains it? Thanks in advance. 回答1: Your proposed way is one way, but as userspace is not within your control (meaning any userspace program have a

2019年12月2日Linux开发手记

試著忘記壹切 提交于 2019-12-06 08:51:00
开始学习在Linux下视频源捕获驱动框架,也就是V4L2(video4linux),本次关于v4l2的知识准备主要在于其的官方例程,理解官方例程也就差不多掌握了v4l2的基本内容。例程在: http://blog.chinaunix.net/uid-23983143-id-3351976.html 知识准备: v42视频编程的流程和对文件操作并没有什么本质的不同,大概的流程如下 1.打开视频设备(通常是/dev/video0) 2.获得设备信息。 3.根据需要更改设备的相关设置。 4.获得采集到的图像数据(在这里v4l提供了两种方式,直接通过打开的设备读取数据,使用mmap内存映射的方式获取数据)。 5.对采集到的数据进行操作(如显示到屏幕,图像处理,存储成图片文件)。 6.关闭视频设备。 知道了流程之后,我们就需要根据流程完成相应的函数。 第一步: 那么我们首先完成第1步打开视频设备,需要完成int v4l_open(char *, v4l_device *); 具体的函数如下 #define DEFAULT_DEVICE “/dev/video0” int v4l_open(char *dev , v4l_device *vd) { if(!dev)dev= DEFAULT_DEVICE; if((vd-fd=open(dev,O_RDWR))<0){perror(“v4l

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

拥有回忆 提交于 2019-12-06 08:27:28
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 Useless for Multi-Threaded Programming . You should not use volatile when changing an integer from more

mmap flag MAP_UNINITIALIZED not defined

若如初见. 提交于 2019-12-06 05:58:10
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 0 in cases where not supported, except in my cases it's not defined at all. In order to understand

《Glinc内存管理》笔记DAY1

风流意气都作罢 提交于 2019-12-06 05:41:25
x86_64栈和mmap固定映射地址   只需要设置全局变量randomize_va_space 值 为 0 , 这 个 变 量 默 认 值 为 1 。 用 户 可 以 通 过 设 置/proc/sys/kernel/randomize_va_space 来停用该特性,也可以用如下命令: sudo sysctl -w kernel.randomize_va_space=0 内存的延迟分配   只有在真正访问一个地址的时候才建立这个地址的物理映射,这是 Linux 内存管理的基本思想之一。Linux 内核在用户申请内存的时候,只是给它分配了一个线性区(也就是虚拟内存),并没有分配实际物理内存;只有当用户使用这块内存的时候,内核才会分配具体的物理页面给用户,这时候才占用宝贵的物理内存。内核释放物理页面是通过释放线性区,找到其所对应的物理页面,将其全部释放的过程。 内核数据结构 mm_struct start_code:进程代码段的起始地址 end_code:进程代码段的终止地址 start_stack:进程堆栈段起始地址 start_brk:进程动态内存分配起始地址(堆的起始地址) brk:是动态内存分配当前的终止地址(堆的当前最后地址) Heap 操作相关函数 #include <unistd.h> 系统调用int brk(void *addr); C库函数void *sbrk