Mmap问题-> segfault

| 我想分享使用mmap。但是它不起作用,因为我遇到了段错误:
int fdL = open(\"/dev/zero\", O_RDWR | O_CREAT);
int *ligneC = (int *) mmap(0, sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED, fdL, 0);

*ligneC = 0;
我哪里错了?     
已邀请:
您的代码对我来说很好。尝试在代码中添加一些错误检查。您将知道失败的原因以及原因:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <string.h>

int main(int argc,char*argv[])
{
    int fdL = open(\"/dev/zero\", O_RDWR | O_CREAT);

    if(fdL<0)
    {
        perror(\"open\");
        exit(1);
    }

    int *ligneC = (int *) mmap(0, sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED, fdL, 0);

    if(ligneC==(int*)-1)
    {
        perror(\"mmap\");
        exit(1);
    }

    *ligneC = 0;
    return 0;
}
    

要回复问题请先登录注册