3

我正在使用该O_DIRECT标志直接从用户缓冲区写入磁盘。但据我了解,Linux 不保证在这个调用之后,数据被写入。它只是使用 DMA 或其他任何东西直接从用户缓冲区写入物理设备......因此,我不明白在调用“写入”函数后是否可以写入用户缓冲区。

我确信示例代码将有助于理解我的问题:

char *user_buff = malloc(...); /* assume it is aligned as needed */
fd = open(..., O_DIRECT);
write(fd, ...)
memset(user_buff, 0, ...)

最后一行(memset)合法吗?写入用户缓冲区是否有效,DMA 可能使用该缓冲区将数据传输到设备?

4

2 回答 2

2

It is legal. There's no "loopback" taking place here - consider what you should do if you used a dynamically mallocated array. Could you free it after write()? Couldn't you? - Well, the answer is that the write() function (and syscall) doesn't modify or access the user buffer after it has returned - if immediate write couldn't be performed, a copy of the data will be made. You don't have to worry about implementation details (that's the purpose of having a C standard library, after all...)

于 2012-12-01T11:04:05.060 回答
1

O_SYNC标志与 结合使用O_DIRECT。然后就可以确定write()返回时数据已经写入了。

于 2012-12-01T11:29:58.620 回答