我想在 C(创建映射文件并仅从中读取)和 python(写入和填充映射文件)之间创建一个共享映射文件。我有这个 C 代码:
#include <sys/types.h>
#include <sys/mman.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
char *shared;
int fd = -1;
fd = open("hello.txt", O_RDWR, 0);
if (fd == -1) {
printf("unable to open");
return 0;
}
shared = (char *)mmap(NULL, 1, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
system("python /home/giuseppe/Documents/test_mmap/mappalo.py");
printf("C CODE: %c\n",shared[0]);
}
这是一个python代码:
import sys
import os
import mmap
with open( "hello.txt", "wb" ) as fd:
fd.write("1")
with open( "hello.txt", "r+b" ) as fd:
mm = mmap.mmap(fd.fileno(), 0, flags=mmap.MAP_SHARED, access=mmap.ACCESS_WRITE, offset=0)
print("content read from file")
print(mm.readline())
mm[0] = "01"
mm.close()
fd.close()
当我执行 C 代码时,我得到这个错误:
content read from file
1
Traceback (most recent call last):
File "/home/giuseppe/Documents/test_mmap/mappalo.py", line 11, in <module>
mm[0] = "01"
IndexError: mmap assignment must be single-character string
C CODE: 1
如何从 python 代码中写入映射文件中的长字符串,然后从 C 代码中读取它?
非常感谢您的参与
更新 我想写两个字符串,所以我写了这段代码:
arr = bytes("Hello123;", 'utf-8')
arr1 = bytes("Hello412;", 'utf-8')
size = sys.getsizeof(arr) + sys.getsizeof(arr1)
with buf:
buf[:size] = struct.pack('ss', arr1, arr)
但我有以下错误:
IndexError: mmap slice assignment is wrong size
更新 2 感谢 Sam Mason 让我解决问题。这是代码:
C代码:
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int fd = open("hello.txt", O_RDWR | O_CREAT, 0666);
if (fd == -1) {
perror("unable to open");
return 1;
}
if (ftruncate(fd, 4096) < 0) {
perror("unable to set length");
return 1;
}
int *shared = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
//system("python3.7 mappalo.py");
printf("C CODE: %s\n", shared);
return 0;
}
小怪兽爱吃肉
相关分类