上下文
代码
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
int main() {
int fd = open( "/tmp/mapped.bin", O_CREAT|O_RDWR|O_SYNC, 0666 );
perror("open");
unsigned char * dst =
(unsigned char *)mmap(
NULL, 64*1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
perror("mmap");
static unsigned char src[] = { 1, 3, 5, 7 };
printf("memcpy( %p, {%d,%d,%d,%d}, %zd )\n", dst, src[0], src[1], src[2], src[3], sizeof(src));
memcpy(dst, src, sizeof(src));
perror("memcpy");
return 0;
}
生产
$ gcc -W -Wall -std=c11 src/org/hpms/filemap/BusError.c -o bin/BusError
(no warning, no error)
执行
$ bin/BusError
open: Success
mmap: Success
memcpy( 0x7f51291fe000, {1,3,5,7}, 4 )
Erreur du bus
GDB
(gdb) run
Starting program: /home/aubin/Dev/Java/2017/org.hpms.filemap/bin/BusError
open: Success
mmap: Success
memcpy( 0x7ffff7fe6000, {1,3,5,7}, 4 )
Program received signal SIGBUS, Bus error.
0x00000000004005ac in main () at src/org/hpms/filemap/BusError.c:15
15 memcpy(dst, src, sizeof(src));
(gdb)
答案 0 :(得分:3)
如果您访问文件外的mmaped内存,您将获得SIGBUS。换句话说,如果要写4个字节,则文件必须至少为4个字节。
在您的示例中,您可以使用ftruncate
解决问题:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
int main() {
int fd = open( "/tmp/mapped.bin", O_CREAT|O_RDWR|O_SYNC, 0666 );
perror("open");
unsigned char * dst =
(unsigned char *)mmap(
NULL, 64*1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
perror("mmap");
static unsigned char src[] = { 1, 3, 5, 7 };
ftruncate(fd, sizeof src);
perror("ftruncate");
printf("memcpy( %p, {%d,%d,%d,%d}, %zd )\n", dst, src[0], src[1], src[2], src[3], sizeof(src));
memcpy(dst, src, sizeof(src));
perror("memcpy");
return 0;
}