简体   繁体   English

从磁盘和EINTR读取

[英]read from disk and EINTR

is it necessary to check for errno == EINTR if you read massive amounts of data? 如果您读取大量数据,是否有必要检查errno == EINTR I use the pread() function to read. 我使用pread()函数进行读取。 In all my time I have never seen EINTR returned, but I have seen some code online where it is explicitely checks for it. 在我所有的时间里,我从未见过EINTR返回,但是我在网上看到了一些明确检查它的代码。

so really is it necessary to check for EINTR and maybe repeat the call? 因此是否真的有必要检查EINTR并可能重复通话?

EINTR is returned when as system call is interrupted as a result of your process receiving a signal. 当系统调用由于您的进程收到信号而中断时,将返回EINTR。 If your process was blocked in the kernel, waiting for the read to complete, and a signal is caught, this may wake the kernel; 如果您的进程在内核中被阻塞,等待读取完成,并且捕获到信号,则可能会唤醒内核; 否则可能会唤醒内核。 this depends on if the operation is interruptable. 这取决于操作是否可中断。 The sleeping I/O routine is woken and is expected to return EINTR to user-space. 睡眠的I / O例程被唤醒,并有望将EINTR返回到用户空间。

Just before the kernel returns to user space, it checks for pending signals. 在内核返回用户空间之前,它会检查未决的信号。 If a signal is pending, it will take the action associated with that signal. 如果有信号待处理,它将采取与该信号相关的操作。 Possible actions include: dispatching the signal to a signal handler, killing your process, or ignoring the signal. 可能的操作包括:将信号调度到信号处理程序,终止进程或忽略该信号。 Assuming this does not kill your process and/or your signal handler returns normally, the system call will return EINTR. 假设这不会杀死您的进程和/或信号处理程序正常返回,则系统调用将返回EINTR。

If you were not expecting this, you typically want to try the action again, but this can also be used as a way to gracefully abort an I/O operation. 如果您不期望这样做,通常您希望再次尝试该操作,但这也可以用作正常中止I / O操作的一种方式。 For example, alarm(2) can be used to implement a timeout, where SIGALRM is delivered if the I/O does not complete in a timely manner. 例如,alarm(2)可用于实现超时,如果I / O没有及时完成,则SIGALRM会被传递出去。 In your signal handler, you could set a flag indicating a timeout and when your read operation returns EINTR, you can check for your timeout flag. 在信号处理程序中,可以设置一个指示超时的标志,并且当您的读取操作返回EINTR时,您可以检查超时标志。

The reason is - on a busy system, for example, it is possible to have an interrupt on the read. 原因是-例如,在繁忙的系统上,读取时可能会中断。 So, on your desktop you may never see it. 因此,在桌面上您可能永远看不到它。 On an overloaded server, you can. 在过载的服务器上,可以。

Se Chapter 5 of Advanced Programming in the UNIX Environment - Stevens and Rago. UNIX环境中高级编程的第5章-Stevens和Rago。 There is a complete explanation. 有完整的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM