Is there a cleaner way to use the write() function reliably?

前端 未结 4 1767
悲哀的现实
悲哀的现实 2021-02-09 16:15

I read the man pages, and my understanding is that if write() fails and sets the errno to EAGAIN or EINTR, I may

4条回答
  •  猫巷女王i
    2021-02-09 16:42

    EINTR and EAGAIN handling should often be slightly different. EAGAIN is always some kind of transient error representing the state of the socket buffer (or perhaps, more precisely, that your operation may block).

    Once you've hit an EAGAIN you'd likely want to sleep a bit or return control to an event loop (assuming you're using one).

    With EINTR the situation is a bit different. If your application is receiving signals non-stop, then it may be an issue in your application or environment, and for that reason I tend to have some kind of internal eintr_max counter so I am not stuck in the theoretical situation where I just continue infinitely looping on EINTR.

    Alnitak's answer (sufficient for most cases) should also be saving errno somewhere, as it may be clobbered by perror() (although it may have been omitted for brevity).

提交回复
热议问题