何时检查EINTR并重复该函数调用?

我正在为一个embedded式Linux系统编写一个用户应用程序,而且我正在使用这些设备的常用function,如打开,closures,读取,ioctl等。 现在,我读了EINTR,表明这个函数被一个信号中断了,但是我不确定它的含义。 在所有的示例程序中,有时会完成,例如ioctl(),有时不完成,例如read()。 所以,我有点困惑。

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

请参阅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. 

默认情况下,你有SA_RESTART行为,所以你不必担心EINTR,如果你不玩信号。

这个链接有一个很好的解释。

http://www.ibm.com/developerworks/linux/library/l-reent.html

干杯!

你的应用程序事件驱动? (意思是它的主循环包括select()/epoll_wait()调用)。

在事件驱动的应用程序中,您可以阻止所有信号,并且只在pselect()/epoll_pwait()调用期间解除它们的阻塞。 这样,你的代码的其余部分永远不必处理EINTR。

在使用read()等待来自命名pipe道的input时,我遇到了类似的问题。

我在GNU libc文档中发现了一个原语的解释和一个有用的macros:TEMP_FAILURE_RETRY

例:

 TEMP_FAILURE_RETRY (read_return = read((int)example_fifo, buffer, (size_t)n)); if (read_return==-1){ fprintf(stderr, "reader.c: read_fifo: read(): %s \n", strerror(errno)); fflush(stderr); } 
Interesting Posts