码:
int main(int argc,char* argv[]) {int pID = getpID();char cmd [1024];int file;if (argc != 2) { printf("Wrong arguments\n"); return 1;}printf("%s",argv[1]);int numpages = atoi(argv[1]);sprintf(cmd,"cat /proc/%d/maps",pID);system(cmd);long page_size = sysconf(_SC_PAGESIZE);file = open(argv[1],O_RDWR,(mode_t)0600);ftruncate(file,page_size * numpages);if(file == -1) { perror("file open Failed!\n"); return 1;}printf("\n\n mapPing file - numpages: %d \n\n",numpages);printf("mapPing %ld KB\n",page_size * numpages/1024);int* mapped_file = mmap(0,page_size * numpages,PROT_READ | PROT_WRITE,MAP_PRIVATE,file,0 );if (mapped_file == MAP_Failed) { perror("Map Failed"); return 1;}printf("mapped file at %p\n\n",mapped_file);int* current_pos = mapped_file;system(cmd);int j;for (j=0; j<numpages; j++) { printf("%d ",*current_pos); current_pos += page_size/4;}if (munmap (mapped_file,page_size * numpages) == -1) { perror ("munmap"); return 1;}if (close(file) == -1) { perror("close"); return 1;}return 0;}
输出:
解决方法 问题是你用O_Trunc打开文件会截断它,因为mmap只能映射文件的现有内容,所以它会给你一个sigbus.在映射文件之前,使用truncate()(和朋友)将文件截断为正确的大小.这让我感到困惑,因为你可以在地图输出中清楚地看到文件已被映射并且地址是正确的 – 但每当我尝试访问它时它都会出错.
编辑:Valgrind:
更新:所以我已经使用ftruncate调用阻止了它崩溃(感谢:))但是它打印全部为零:
jords@jords-desktop ~/engcode/workspace/SE370Assignment3 (master) $./A3Program3 6400400000-00401000 r-xp 00000000 08:03 3288976 /home/jords/engcode/workspace/SE370Assignment3/A3Program30(gdb) run 64Starting program: /home/jords/engcode/workspace/SE370Assignment3/A3Program3 6400400000-00401000 r-xp 00000000 08:03 3288976 /home/jords/engcode/workspace/SE370Assignment3/A3Program3000-00601000 r--p 00000000 08:03 3288976 /home/jords/engcode/workspace/SE370Assignment3/A3Program3
00601000-00602000 rw-p 00001000 08:03 3288976 /home/jords/engcode/workspace/SE370Assignment3/A3Program3
7f96ec7a1000-7f96ec92b000 r-xp 00000000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7f96ec92b000-7f96ecb2a000 ---p 0018a000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7f96ecb2a000-7f96ecb2e000 r--p 00189000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7f96ecb2e000-7f96ecb2f000 rw-p 0018d000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7f96ecb2f000-7f96ecb35000 rw-p 00000000 00:00 0
7f96ecb35000-7f96ecb56000 r-xp 00000000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7f96ecd2a000-7f96ecd2d000 rw-p 00000000 00:00 0
7f96ecd52000-7f96ecd55000 rw-p 00000000 00:00 0
7f96ecd55000-7f96ecd56000 r--p 00020000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7f96ecd56000-7f96ecd58000 rw-p 00021000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7fff9be10000-7fff9be31000 rw-p 00000000 00:00 0 [stack]
7fff9bf0b000-7fff9bf0c000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
64映射文件 – numpages:64
映射256 KB
映射文件位于0x7f96eccea00000400000-00401000 r-xp 00000000 08:03 3288976 / home / jords / engcode / workspace / SE370Assignment3 / A3Program3
000-00601000 r – p 00000000 08:03 3288976 / home / jords / engcode / workspace / SE370Assignment3 / A3Program3
00601000-00602000 rw-p 00001000 08:03 3288976 / home / jords / engcode / workspace / SE370Assignment3 / A3Program3
7f96ec7a1000-7f96ec92b000 r-xp 00000000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7f96ec92b000-7f96ecb2a000 — p 0018a000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7f96ecb2a000-7f96ecb2e000 r – p 00189000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7f96ecb2e000-7f96ecb2f000 rw-p 0018d000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7f96ecb2f000-7f96ecb35000 rw-p 00000000 00:00 0
7f96ecb35000-7f96ecb56000 r-xp 00000000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7f96eccea000-7f96ecd2a000 rw-p 00000000 08:03 3288973 / home / jords / engcode / workspace / SE370Assignment3 / 64
7f96ecd2a000-7f96ecd2d000 rw-p 00000000 00:00 0
7f96ecd52000-7f96ecd55000 rw-p 00000000 00:00 0
7f96ecd55000-7f96ecd56000 r – p 00020000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7f96ecd56000-7f96ecd58000 rw-p 00021000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7fff9be10000-7fff9be31000 rw-p 00000000 00:00 0 [stack]
7fff9bf0b000-7fff9bf0c000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0我认为这是因为文件没有被正确读取,所以我正在阅读的是填充零.但我不确定为什么会这样
更新,解决了,它实际上是正确地读取文件我只是在如何阅读它时犯了一个错误.感谢大家
int main(int argc,page_size * numpages) == -1) { perror ("munmap"); return 1;}if (close(file) == -1) { perror("close"); return 1;}return 0;}
00-00601000 r--p 00000000 08:03 3288976 /home/jords/engcode/workspace/SE370Assignment3/A3Program3
00601000-00602000 rw-p 00001000 08:03 3288976 /home/jords/engcode/workspace/SE370Assignment3/A3Program3
7ffff7a48000-7ffff7bd2000 r-xp 00000000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7ffff7bd2000-7ffff7dd1000 ---p 0018a000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7ffff7dd1000-7ffff7dd5000 r--p 00189000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7ffff7dd5000-7ffff7dd6000 rw-p 0018d000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7ffff7dd6000-7ffff7ddc000 rw-p 00000000 00:00 0
7ffff7ddc000-7ffff7dfd000 r-xp 00000000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7ffff7fd0000-7ffff7fd3000 rw-p 00000000 00:00 0
7ffff7ff8000-7ffff7ffb000 rw-p 00000000 00:00 0
7ffff7ffb000-7ffff7ffc000 r-xp 00000000 00:00 0 [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00020000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7ffff7ffd000-7ffff7fff000 rw-p 00021000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
64mapPing file - numpages: 64
mapPing 256 KB
mapped file at 0x7ffff7ff400000400000-00401000 r-xp 00000000 08:03 3288976 /home/jords/engcode/workspace/SE370Assignment3/A3Program3
0int main(int argc,page_size * numpages) == -1) { perror ("munmap"); return 1;}if (close(file) == -1) { perror("close"); return 1;}return 0;}
00-00601000 r--p 00000000 08:03 3288976 /home/jords/engcode/workspace/SE370Assignment3/A3Program3
00601000-00602000 rw-p 00001000 08:03 3288976 /home/jords/engcode/workspace/SE370Assignment3/A3Program3
7ffff7a48000-7ffff7bd2000 r-xp 00000000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7ffff7bd2000-7ffff7dd1000 ---p 0018a000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7ffff7dd1000-7ffff7dd5000 r--p 00189000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7ffff7dd5000-7ffff7dd6000 rw-p 0018d000 00:10 8103 /lib/x86_64-linux-gnu/libc-2.13.so
7ffff7dd6000-7ffff7ddc000 rw-p 00000000 00:00 0
7ffff7ddc000-7ffff7dfd000 r-xp 00000000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7ffff7fd0000-7ffff7fd3000 rw-p 00000000 00:00 0
7ffff7ff4000-7ffff7ff8000 rw-p 00000000 08:03 3288973 /home/jords/engcode/workspace/SE370Assignment3/64
7ffff7ff8000-7ffff7ffb000 rw-p 00000000 00:00 0
7ffff7ffb000-7ffff7ffc000 r-xp 00000000 00:00 0 [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00020000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7ffff7ffd000-7ffff7fff000 rw-p 00021000 00:10 8090 /lib/x86_64-linux-gnu/ld-2.13.so
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]Program received signal SIGBUS,Bus error.
int main(int argc,page_size * numpages) == -1) { perror ("munmap"); return 1;}if (close(file) == -1) { perror("close"); return 1;}return 0;}int main(int argc,page_size * numpages) == -1) { perror ("munmap"); return 1;}if (close(file) == -1) { perror("close"); return 1;}return 0;}ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]==16295== ==16295== Process terminating with default action of signal 7 (SIGBUS)==16295== Non-existent physical address at address 0x4024000==16295== at 0x400AB7: main (A3Program3.c:49)==16295==int main(int argc,page_size * numpages) == -1) { perror ("munmap"); return 1;}if (close(file) == -1) { perror("close"); return 1;}return 0;}
0x0000000000400abb in main (argc=2,argv=0x7fffffffe058) at A3Program3.c:49
49 printf("%d",mapped_file[0]);
(gdb)
从mmap的手册页:
SIGBUS Attempted access to a portion of the buffer that does not correspond to the file (for example,beyond the end of the file,including the case where another process has truncated the file).总结
以上是内存溢出为你收集整理的mmap不存在物理地址错误全部内容,希望文章能够帮你解决mmap不存在物理地址错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)