开发者

Linux Write Function

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:

  1. How large is the file buffer size until it is blocked?

  2. If I am using O_NONBLOCK as above, what would happen if the buffer is already full? The string would be dropped开发者_如何学JAVA?

  3. 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?

  4. 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".


  1. This is purposefully left unspecified, and your code should not know or care about that.
  2. write() will return a number that is less than size, or even zero, in which case errno will be set to EAGAIN.
  3. Definitely. And retry from where the last write() left.
  4. Try writing to standard output (fd = 1) and run your program through a pipe, ie

myprogram | (sleep 30; cat)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜