2010-08-23 81 views
1

我分配一些內存匿名映射:直接IO的mmap:地址不好?

buff->addr = mmap(NULL, length, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS -1, 0); 
fprintf(stderr, "allocated buffer: %p, %lu\n", buff->addr, (unsigned long)length); 

然後我用FD寫入它打開與O_DIRECT:

int fd = open(name, O_CREAT | O_TRUNC | O_WRONLY | O_DIRECT, 00300); 
if(fd == -1) 
{ 
    perror("failed to open for write"); 
    return; 
} 

fprintf(stderr, "writing to address: %p size: %lu\n", buff.addr, buff.length); 
if(write(fd, buff.addr, buff.length) != size) 
{ 
    perror("failed to write"); 
} 

,但得到錯誤的地址錯誤:

allocated buffer: 0x7f096ddff000, 512000 
writing to address: 0x7f096ddff000 size: 512000 
failed to write: Bad address 

地址對我來說很好。

如果我分配中。數據緩衝和調整到512,寫作品,所以開()和write()都OK,我擁有所有權限等

有什麼不對?直接IO比512對齊還有其他要求嗎?

UPDATE:

例如,如果我改變了這個MMAP:

char buffer[ 1024000 ]; 
for(buff->addr = buffer; ((unsigned long)buff->addr % 512) != 0; buff->addr = buff->addr + 1); 

它的工作原理:

allocated buffer: 0x500e00, 512000 
writing to address: 0x500e00 size: 512000 
+0

'write()'返回一個非零值嗎? – 2010-08-23 18:32:07

+0

你想寫入一些硬件/外設?這是你直接IO的意思嗎? – Longfield 2010-08-23 18:38:08

+0

寫是應該返回非零值,是的。這裏它通過直接IO返回一個錯誤 – 2010-08-23 18:44:55

回答

3

PROT_NONE Pages may not be accessed.

你不應該ATLEAST有PROT_READ爲3 。參數mmap?在這種情況下,你可能需要PROT_READ | PROT_WRITE

+0

我誤讀了這個人。非常感謝 – 2010-08-23 18:53:12