在 C mmap 中写入文件:权限被拒绝。 Linux

In C mmap the file for write: Permission denied. Linux

这是我的代码:

   #include <stdio.h>
   #include <stdlib.h>
   #include <fcntl.h>
   #include <sys/types.h>
   #include <sys/stat.h>
   #include <sys/mman.h>

   int main()
   {
    int fd=open("/home/victor/hello",O_WRONLY);

    if(fd<0)
    {
            perror("Open");
            exit(EXIT_FAILURE);
    }

    struct stat sbuf;

    if(fstat(fd, &sbuf)==-1){
            perror("stat");
            close(fd);
            exit(EXIT_FAILURE);
    }

    void* file_memory= mmap(NULL, sbuf.st_size, PROT_WRITE, MAP_SHARED,fd,0);
    if (file_memory == MAP_FAILED ) {
            perror("Error mmapping the file");
            close(fd);
            exit(EXIT_FAILURE);
    }

    return 0;
   }

我也试过了

    int fd=open("/home/victor/hello",O_WRONLY|0777);

但还是一样的错误:

映射文件时出错:权限被拒绝

执行 ls -l | grep hola -rwxrwxrwx 1 victor victor 24 十月 24 01:47 你好

怎么了?

来自 glibc 手册,正如上面 R..Iwillnotexist Idonotexist 所指出的:

Note that most hardware designs cannot support write permission without read permission, and many do not distinguish read and execute permission. Thus, you may receive wider permissions than you ask for, and mappings of write-only files may be denied even if you do not use PROT_READ.

http://www.gnu.org/software/libc/manual/html_node/Memory_002dmapped-I_002fO.html