When does the write() system call write all of the requested buffer versus just doing a partial write?

后端 未结 3 674
无人及你
无人及你 2021-02-09 21:55

If I am counting on my write() system call to write say e.g., 100 bytes, I always put that write() call in a loop that checks to see if the length that gets returned is what I e

3条回答
  •  星月不相逢
    2021-02-09 22:29

    You need to check errno to see if your call got interrupted, or why write() returned early, and why it only wrote a certain number of bytes.

    From man 2 write

    When using non-blocking I/O on objects such as sockets that are subject to flow control, write() and writev() may write fewer bytes than requested; the return value must be noted, and the remainder of the operation should be retried when possible.

    Basically, unless you are writing to a non-blocking socket, the only other time this will happen is if you get interrupted by a signal.

    [EINTR] A signal interrupted the write before it could be completed.

    See the Errors section in the man page for more information on what can be returned, and when it will be returned. From there you need to figure out if the error is severe enough to log an error and quit, or if you can continue the operation at hand!

    This is all discussed in the book: Advanced Unix Programming by Marc J. Rochkind, I have written countless programs with the help of this book, and would suggest it while programming for a UNIX like OS.

提交回复
热议问题