m地图用例,照着书下的也错,提示m地图:invalid argument

mmap用例,照着书上的也错,提示mmap:invalid argument!
C/C++ code

#include<stdlib.h>
#include<sys/mman.h>
#include<fcntl.h>

int main(void)
{
    int *p;
   
    int fd = open("hello", O_RDWR);
    if(fd < 0)
    {
        perror("open hello");
        exit(1);
    }
   
    p = mmap(NULL,6, PROT_WRITE, MAP_SHARED, fd, 0);
    if(p == MAP_FAILED)
    {
       perror("mmap"); //程序进里面了,证明mmap失败
       exit(1);
    }

    close(fd);
    p[0] = 0x30313233;
    munmap(p, 6);
    return 0;
}




我的同目录下的hello文件中有顶格开始有hello字样,开始以为长度太短,改成helloll也不行。
百度和****搜了都没找到答案,谢谢

------解决方案--------------------
用lz的程序试验了一下,运行正常
------解决方案--------------------
offset必须是pagesize的整数倍,len可是不是pagesize整数倍,会由mmap主动调整,相关manpage如下:

The off argument is constrained to be aligned and sized according to the value returned by sysconf() when passed _SC_PAGESIZE or _SC_PAGE_SIZE. When MAP_FIXED
is specified, the application shall ensure that the argument addr also meets these constraints. The implementation performs mapping operations over whole
pages. Thus, while the argument len need not meet a size or alignment constraint, the implementation shall include, in any mapping operation, any partial page
specified by the range [pa,pa+len).


NAME
getpagesize - get memory page size

SYNOPSIS
#include <unistd.h>

int getpagesize(void);

#include <sys/mman.h>

void *mmap(void *addr, size_t len, int prot, int flags,
int fildes, off_t off);

楼主的文件创建时指定读写权限了?