Checking if errno != EINTR: what does it mean?

我怕爱的太早我们不能终老 提交于 2019-11-27 02:45:53

问题


I've found this piece of code used several times (also a similar one where it's used open() instead of write()).

int c = write(fd, &v, sizeof(v));
if (c == -1 && errno != EINTR) {
    perror("Write to output file");
    exit(EXIT_FAILURE);
}

Why it is checked if && errno != EINTR here ?

Looking for errno on man I found the following text about EINTR, but even if I visited man 7 signal that doesn't enlighten me.

EINTR Interrupted function call (POSIX.1); see signal(7).


回答1:


Many system calls will report the EINTR error code if a signal occurred while the system call was in progress. No error actually occurred, it's just reported that way because the system isn't able to resume the system call automatically. This coding pattern simply retries the system call when this happens, to ignore the interrupt.

For instance, this might happen if the program makes use of alarm() to run some code asynchronously when a timer runs out. If the timeout occurs while the program is calling write(), we just want to restart it.




回答2:


the answers here are really good and i want to add some internal details :

System calls that are interrupted by signals can either abort and return EINTR or automatically restart themselves if and only if SA_RESTART is specified in sigaction(2)

and the one responsible for this task is the restart_block which used to track information and arguments for restarting system calls




回答3:


From the man page on write:

The call was interrupted by a signal before any data was written



来源:https://stackoverflow.com/questions/41474299/checking-if-errno-eintr-what-does-it-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!