什么是错误的struct和mmap

时间:2012-02-16 22:48:10

标签: c queue mmap

我有一个tailq结构:

struct entry {
    int file;
    int *map;
    int pos;
    TAILQ_ENTRY(entry) tailq;         /* Tail queue. */
};

对于tailq的每个条目,我都有一个mmaped文件,或者希望:

#define NUMINTS  (1000)
#define FILESIZE (NUMINTS * sizeof(u_int64_t))

struct entry *np;
int result;

if((np = malloc(sizeof(struct entry))) == NULL){
    errx(1, "malloc");
}

np->file = open(temp, O_WRONLY | O_CREAT, (mode_t)0600);

if (np->file == -1) {
    errx(1, "Error opening file for writing");
}

np->map = mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, np->file, 0);

if (np->map == MAP_FAILED) {
    close(np->file);
    errx(1, "Error mmapping the file");
}

TAILQ_INSERT_TAIL(&tailq_head[thread_id], np, tailq);

我收到了“错误的mmapping文件”,为什么?

1 个答案:

答案 0 :(得分:1)

您正在以只写方式打开文件,然后尝试映射读/写。尝试打开文件O_RDWR