何时检查EINTR并重复功能调用?

ste*_*ter 38 linux eintr

我正在为嵌入式Linux系统编写用户应用程序,我正在使用常用功能,如open,close,read,ioctl等设备.现在,我读到了有关EINTR的信息,表明该功能被信号中断,但我不确定其含义.在我所有的示例程序中,有时它已完成,例如ioctl(),有时它没有完成,例如read().所以,我有点困惑.

我什么时候最好检查EINTR并重复功能调用?

Yan*_*aud 15

请参阅sigaction:http://pubs.opengroup.org/onlinepubs/009695399/functions/sigaction.html

SA_RESTART
  This flag affects the behavior of interruptible functions; that is, those 
  specified to fail with errno set to EINTR. If set, and a function specified 
  as interruptible is interrupted by this signal, the function shall restart 
  and shall not fail with EINTR unless otherwise specified. If the flag is not 
  set, interruptible functions interrupted by this signal shall fail with errno 
  set to EINTR.
Run Code Online (Sandbox Code Playgroud)

默认情况下,您具有SA_RESTART行为,因此如果不使用信号,则不必担心EINTR.

  • @stefangachter正如您所总结的那样,重要的是要注意SA_RESTART并不总是适用.无论是否使用SA_RESTART,某些接口在被信号处理程序中断后都不会重新启动; 当信号处理程序中断时,它们总是因错误EINTR而失败.查看[信号手册页以获取详细信息](http://man7.org/linux/man-pages/man7/signal.7.html). (5认同)
  • @stefangachter其实有时你会这样做.*"在Linux上,即使在没有信号处理程序的情况下,某些阻塞接口也会因为其中一个停止信号停止进程而导致错误EINTR失败,然后通过SIGCONT恢复.这种行为不受POSIX.1的批准,并且在其他系统上不会出现这种情况."*正如kikeenrique所指出的那样,请参阅`signal(7)`以了解具体的Linux特定信息. (4认同)
  • 在花了一些时间阅读之后,我得出结论,我必须检查中断的系统调用,SA_RESTART标志不考虑这些调用. (3认同)