mmap3-map the ordinary file-segmentation fault

//this is the file 3.c
#include <sys/mman.h>
#include<stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<malloc.h>
#include <string.h>
int main(int argc, char** argv)
{
    int fd;
    int i;
    char *start;
    fd = open(argv[1],O_RDWR);
    if (fd<0)
    {
        printf("open file  error /n");
        return 0;
    }
   start=mmap((void*)0xbcde6600,10, PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0);
    if (start!=NULL)
    {
        printf("mmap success p=%x\n",start);
    }
/*
for(i=0;i<20;i++)
{	
	printf("address= %x\n",start);

	printf("content= %d\n",*start);
	printf("content= %c\n\n",*start);
	start++;
}	
*/
printf("start= %x\n",start);//output the start as a hex number
printf("start= %s\n",start);//output the start as a string
getchar() ;
    munmap(start,10);
    close(fd);
    return 0;
}
[root@localhost mmap]# ./3 text
mmap success p=bcde6000
start= bcde6000
start= hello,evreyone

ok,let me exchange a text file

any text file with the size of largger than 4096B(4KB),for example text1

-rw-r--r-- 1 root root   4097 2011-07-07 03:49 text1

run it
[root@localhost mmap]# ./3 text1
mmap success p=bcde6000
start= bcde6000
Segmentation fault
[root@localhost mmap]# 
segment  fault---shit,非法内存访问,应该就像windows下的您访问的内存不能为read一个级别的错误信息(内核发出的)

if i remve 2 characters from text1,

-rw-r--r-- 1 root root   4095 2011-07-07 03:53 text1
[root@localhost mmap]# ./3 text1
mmap success p=bcde6000
start= bcde6000
start= The text of and illustrations in this document arelicensed by Red Hat under a Creative Commons Attribution–Share Al

  
  
ike3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA isavailable at http://creativecommons.org/licenses/by-sa/3.0/.The  original authors of this document, and Red Hat, designate theFedora Project as the "Attribution Party" for purposes of CC-BY-S A. Inaccordance with CC-BY-SA, if you distribute this document or anadaptation of it, you must provide the URL for the origin al version.
Red Hat, as the licensor of this document,waives the ri ght to enforce, and agrees not to assert, Section 4d ofCC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, theShadowman logo, JBoss, Me taMatrix, Fedora, the Infinity Logo, and RHCEare trademarks of Red Hat, Inc., registered in the United States andother countries.
..............

it's ok,

if the file is largger than the length of allocted page in size,when i try to read the file as a string totally,"segment  fault"  will occur .

actually,when reading the data out of the allocted page(4KB) ,"segment  fault"  will occur 


   start=mmap((void*)0xbcde6600,10, PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0);
the second argument is set to 10----indicates the allocted one page(4KB)

we can first get the size of file using fstat function,and pass the size to the second arg,

the modifed code is

//this is the file 3.c
#include <sys/mman.h>
#include<stdio.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<malloc.h>
#include <string.h>
int main(int argc, char** argv)
{
    struct stat buf;
    int fd;
    int i;
    char *start;
    fd = open(argv[1],O_RDWR);
    fstat(fd,&buf);
    if (fd<0)
    {
        printf("open file  error /n");
        return 0;
    }
   start=mmap((void*)0xbcde6600,buf.st_size, PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,0);
    if (start!=NULL)
    {
        printf("mmap success p=%x\n",start);
    }
/*
for(i=0;i<4200;i++)
{	
	printf("address= %x\n",start);
	printf("content= %d\n",*start);
	printf("content= %c\n\n",*start);
	start++;
}	
*/
printf("start= %x\n",start);//output the start as a hex number
printf("start= %s\n",start);//output the start as a string
getchar() ;
    munmap(start,10);
    close(fd);
    return 0;
}
and the maps of memory after it running is showed as below

[root@localhost mmap]# cat /proc/13647/maps 
00110000-00111000 r-xp 00110000 00:00 0          [vdso]
007d0000-007ec000 r-xp 00000000 08:05 478778     /lib/ld-2.8.so
007ec000-007ed000 r--p 0001c000 08:05 478778     /lib/ld-2.8.so
007ed000-007ee000 rw-p 0001d000 08:05 478778     /lib/ld-2.8.so
007f0000-00953000 r-xp 00000000 08:05 478779     /lib/libc-2.8.so
00953000-00955000 r--p 00163000 08:05 478779     /lib/libc-2.8.so
00955000-00956000 rw-p 00165000 08:05 478779     /lib/libc-2.8.so
00956000-00959000 rw-p 00956000 00:00 0 
08048000-08049000 r-xp 00000000 08:05 1043845    /root/Desktop/from windows/mmap/3
08049000-0804a000 rw-p 00000000 08:05 1043845    /root/Desktop/from windows/mmap/3
b7f4d000-b7f4f000 rw-p b7f4d000 00:00 0 
b7f6a000-b7f6c000 rw-p b7f6a000 00:00 0 
bcde6000-bcde8000 rw-p 00000000 08:05 1043855    /root/Desktop/from windows/mmap/text1
bfc56000-bfc6b000 rw-p bffeb000 00:00 0          [stack]
now it's 2pages,8Kb
bcde6000-bcde8000 rw-p 00000000 08:05 1043855    /root/Desktop/from windows/mmap/text1




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值