日期:2014-05-16  浏览次数:20684 次

关于mmap的一个问题(一) :(user space)


我按照一个     linux下c     语言函数     帮助测试如下     源程序,结果没有映射成功,大家帮看下    
 
#include <sys/types.h>    
#include <sys/stat.h>    
#include <fcntl.h>    
#include <unistd.h>    
#include <sys/mman.h>    
main()    
{    
int     fd;    
void     *start;    
struct     stat     sb;    
fd=open( "/etc/passwd ",O_RDONLY);     /*open     /etc/passwd     */    
printf( "fd     =     %d     \n ",     fd);    
fstat(fd,&sb);     /*get     file     size/    
start=mmap(NULL,sb.st_size,PROT_READ,MAP_PRIVATE,fd,0);    
if(start     ==     MAP_FAILED)     /*check     that     map     is     ok*/    
{    
      printf( "mmap     failed!!\n ");    
      return;    
}    
printf( "%s ",start);    
munma(start,sb.st_size);     /*unmap*/    
closed(fd);    
}    
 
 
平台是     redhat的         AS4,     编译通过,     运行输出如下:    
 
[root@112     test]#     gcc     mmap.c     -o     mmap                                                                                                                                                                                
[root@112     test]#     ./mmap        
fd     =     3        
mmap     failed!!    
[root@112     test]#    



------解决方案--------------------
munma => munmap
closed => close

我不知道你参考的什么东西,不过参考的东西实在是太烂了。

还有 /etc/passwd 不是随便可以map的,需要权限。
------解决方案--------------------
[root@112 test]# root权限还不够啊??
------解决方案--------------------
你咋会 mmap 那个文件呢。。 好奇怪啊。。

------解决方案--------------------
可以出来啊
你的这个很正常
你file /etc/passwd一下
我的是ASCI