简体   繁体   中英

Python mmap to access shared kernel memory

I have a kernel module in C which reads continuously data from a photiodiode and writes the values together with current time into a memory mapped file. From the C program in ther user space I can access the data from the kernel. I tried to do the same in Python via the mmap functionality. However, when I try to mmap the file I get errors like "mmap length is greater than file size" or "mmap file is empty". It seems that Python cannot access the mmaped file from C, is that correct? In the end, I need a numpy array of the photodiode data for further processing.

Details about kernel data structure: The mmap contains a struct with index to latest voltage values and a struct array with voltage and time. The kernel has one big struct array and writes the photodiode data in chunks of page size into the struct array. The C user space program reads then each chunk for futher processing.

Python code to read mmaped C file:

num_pages = 103
page_size = 10000
max_buffer_size = num_pages * page_size

class buf_element(ctypes.Structure):

    _fields_ = [("voltage", ctypes.c_int),
        ("time", ctypes.c_uint)]

class data(ctypes.Structure):

    _fields_ = [("latest_page_offset", ctypes.c_int),
                ("buffer", ctypes.POINTER(buf_element))]

length_data = ctypes.sizeof(ctypes.c_int) + max_buffer_size * ctypes.sizeof(buf_element);
fd = os.open(data_file, os.O_RDWR)
buf = mmap.mmap(fd, length_data, mmap.MAP_SHARED, mmap.PROT_READ)
test_data = data.from_buffer(buf)
print test_data.latest_page_offset
os.close(fd)

My idea was to use the already existing and working C code from python via C extensions. So, python calls C and hand over a numpy array and C writes the data into that. Is that the fastest way? Other recommendations?

为了使其正常工作,我通过Python的Cython使用C代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM