检查errno!= EINTR:这是什么意思? [英] Checking if errno != EINTR: what does it mean?

查看:555
本文介绍了检查errno!= EINTR:这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这段代码使用了多次(也是使用open()而不是write()的类似代码).

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);
}

为什么要在这里检查&& errno != EINTR?

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

man 上寻找errno时,我发现了以下有关EINTR的文字,但是即使我访问了man 7 signal也没有启发我.

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中断的函数调用(POSIX.1);参见signal(7).

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

推荐答案

如果在进行系统调用时发生信号,许多系统调用将报告EINTR错误代码.实际没有发生错误,只是以这种方式报告,因为系统无法自动恢复系统调用.这种编码模式只会在发生这种情况时重试系统调用,从而忽略中断.

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.

例如,如果程序使用alarm()在计时器用尽时异步运行某些代码,则可能会发生这种情况.如果在程序调用write()时发生超时,我们只想重试系统调用(又名读/写等).

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 retry the system call (aka read/write, etc).

这篇关于检查errno!= EINTR:这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆