I am writing to a text file using the write function with the file descriptor set a O_NONBLOCK.
fd = open(filepath, O_RDWR | O_NONBLOCK , 0777);
write(fd, string, size);
The questions I have are as following:
How large is the file buffer size until it is blocked?
If I am using O_NONBLOCK as above, what would happen if the buffer is already full? The string would be dropped开发者_如何学JAVA?
So for the O_NONBLOCK for write, I should always check the return value of write to see whether it is the same as the length of the string we want to write?
How do I test the phenomena of the file write buffer is full? I have created an arbitary long string in writing but it seems that I still couldn't produce the effect string drop.
Thanks.
O_NONBLOCK
has no effect for file descriptors (*). The write()
s are generally already buffered by OS in the cache and OS/file system itself decides when the data should hit the disk. O_NONBLOCK
should be used only with sockets, fifos and pipes.
If you need async file I/O you should check the aio_write()
.
Otherwise, to reiterate, write()
is already asynchronous and doesn't wait for disk IO to complete unless you use O_SYNC
or O_DSYNC
or O_DIRECT
flags.
(*) Under Linux open()ing a file with O_NONBLOCK
is just a hint that program doesn't intend to read or write to the file - but do only block layer ioctl()
calls.
Edit1. Reality check that I'm not daydreaming. As per POSIXv6:
O_NONBLOCK
When opening a FIFO with O_RDONLY or O_WRONLY set:
[... skip ...]
When opening a block special or character special file that supports non-blocking opens:
[... skip ...]
Otherwise, the behavior of O_NONBLOCK is unspecified.
There are different reasons why the write might not succeed to write completely onto the fd.
U can simulate blocking easily in sockets, pipe etc. where the write would have been blocked if the consumer on the other side has not read from the stream.
If you had opened the fd with NON_BLOCK then write would written immediate with the number of bytes written to the stream. One can use this information to retry the operation for the remaining data. In no case there would be dropping of the "string".
- This is purposefully left unspecified, and your code should not know or care about that.
- write() will return a number that is less than size, or even zero, in which case errno will be set to EAGAIN.
- Definitely. And retry from where the last write() left.
- Try writing to standard output (fd = 1) and run your program through a pipe, ie
myprogram | (sleep 30; cat)
精彩评论