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

查看:32
本文介绍了检查 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);见信号(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天全站免登陆