When to check for EINTR and repeat the function call?

后端 未结 4 1931
攒了一身酷
攒了一身酷 2020-12-05 01:43

I am programming a user application for a embedded Linux system, and I am using the common functions such as open, close, read, ioctl, etc. for the devices. Now, I read abou

4条回答
  •  醉酒成梦
    2020-12-05 02:26

    I know this question is old but I think there's more to be said. To answer the specific question in the title: basically, never.

    Aside from select and poll, EINTR can only happen if you've installed (either by mistake/not understanding how to use sigaction, or because you want to be able to interrupt blocking operations) interrupting signal handlers. Looping to retry when a function fails with EINTR just undoes that. Doing so is an old anti-pattern from the 80s and early to mid 90s when lots of operating systems were buggy and reportedly generated EINTR under non-conforming circumstances, but these bugs are long gone.

    It is possible that, even in an application where you want to be able to interrupt things with signals, there is some code whose successful completion is so important that you can't treat EINTR as an error condition and return back to the caller without completing the operation. In such a circumstance, it might make sense to install a retry loop, but it's probably better to mask signals (at least the potentially-interrupting ones) during the operation and unmasks after it finishes.

提交回复
热议问题