Reputation: 31
I'm using the O_DIRECT
flag to write to the disk directly from the user buffer.
But as far as I understand, Linux doesn't guarantee that after this call, the data is written.
It just writes directly from the user buffer to the physical device using DMA or anything else...
Therefore, I don't understand if I can write to the user buffer after the call to 'write' function.
I'm sure that example code will help to understand my question:
char *user_buff = malloc(...); /* assume it is aligned as needed */
fd = open(..., O_DIRECT);
write(fd, ...)
memset(user_buff, 0, ...)
Is the last line (memset) legal? Is writing to the user buffer valid that is maybe used by DMA to transfer data to the device?
Upvotes: 3
Views: 610
Reputation: 782166
Use the O_SYNC
flag in conjunction with O_DIRECT
. Then you can be sure that the data has been written when write()
returns.
Upvotes: 1
Reputation:
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...)
Upvotes: 2