mmap4-map the ordinary file-bus error.

int the last blog ,if i modify the code as

   start=mmap((void*)0xbcde6600,buf.st_size, PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,1);
the last  argument now is changed into 1 from 0----------indicates that i want  mapping the file from the second byts,----but it's not impossible

run it

[root@localhost mmap]# ./3 text1
mmap success p=ffffffff
start= ffffffff
Segmentation fault
segment fault occurs again!

it seems to be more serious than the last example as the address is fffffff

so i modify again as

   start=mmap((void*)0xbcde6600,buf.st_size, PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,4096);
run it

[root@localhost mmap]# ./3 text1
mmap success p=bcde6000
start= bcde6000
start= 

now it is ok ,but the text1 is 4097KB in size----no string to disply

so type some characters at the end of text1 file as  "this is the end of text1"

-rw-r--r-- 1 root root   4122 2011-07-07 04:53 text1
you know,4122>4096

run it again

[root@localhost mmap]# ./4 text3
open file  error /n[root@localhost mmap]# ./4 text1
mmap ok.p=b7f67000
buf= 
this is the end of text1


it's ok

 we can come to a conclusion from the text above

the file is divided  into sevral parts(likes pages in vertue memory )  by os and every part is also 4KB.

the memory page shoud be aligned with the file part

so we must map the file from one part ,such as  0,4096....,not 1 or 2

ok,now let's make another error just  modify the code

   start=mmap((void*)0xbcde6600,buf.st_size, PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,8192);
the last  argument now is changed into 8192 from 4096,it's following the alinged rule,but the erro comes after runing 

[root@localhost mmap]# ./3 text1
mmap success p=bcde6000
start= bcde6000
Bus error
[root@localhost mmap]# 
bus error,

see the size of file text1

-rw-r--r-- 1 root root   4122 2011-07-07 04:53 text1
4122Bytes  totally,

the part starts from 8192 beyond  4122





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值