写入管道时出现中断系统调用错误

发布于 2024-11-05 14:21:02 字数 385 浏览 3 评论 0原文

在我的用户空间 Linux 应用程序中,我有一个通过管道与主进程通信的线程。下面是代码,

static void _notify_main(int cond)
{
    int r;
    int tmp = cond;

    r = write( _nfy_fd, &tmp, sizeof(tmp) );
    ERROR( "write failed: %d. %s\n",  r, strerror(r) );
}

非常简单。现在已经运行良好有一段时间了。但最近,在程序经过一些压力测试后,写入调用将失败,并出现“系统调用中断”错误。

奇怪的是,这些东西实际上通过管道没有问题。当然,我仍然想找到错误消息的底部并将其删除。

谢谢,

In my user space Linux application, I have a thread which communicated to the main process through a pipe. Below is the code

static void _notify_main(int cond)
{
    int r;
    int tmp = cond;

    r = write( _nfy_fd, &tmp, sizeof(tmp) );
    ERROR( "write failed: %d. %s\n",  r, strerror(r) );
}

Pretty straight forward. It's been working fine for quite a while now. But recently, the write call will fail with "interrupted system call" error after the programme went under some stress test.

Strangely, the stuff actually went through the pipe no problem. Of course I'd still like to go to the bottom of the error message and get rid of it.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2024-11-12 14:21:02

write(2) 手册页提到:

符合
SVr4、4.3BSD、POSIX.1-2001。

在 SVr4 下,写入可能会在任何时候中断并返回 EINTR,而不仅仅是在写入任何数据之前。

我猜你只是幸运,到目前为止还没有发生这种情况。

如果您只是搜索“中断的系统调用”,您会发现此线程,它告诉您可以使用 siginterrupt() 自动重新启动 write 调用。

The write(2) man page mentions:

Conforming to
SVr4, 4.3BSD, POSIX.1-2001.

Under SVr4 a write may be interrupted and return EINTR at any point, not just before any data is written.

I guess you were just lucky that it didn't occur so far.

If you google just for the "interrupted system call", you will find this thread which tells you to use siginterrupt() to auto-restart the write call.

明媚殇 2024-11-12 14:21:02

来自 http://www.gnu.org/

当 I/O 原语(例如 open)时,信号可以到达并被处理
或读取正在等待 I/O 设备。如果信号处理程序返回,
系统面临的问题是:接下来会发生什么?

POSIX 指定了一种方法:使原语立即失败。这
此类故障的错误代码是 EINTR。这很灵活,但是
通常不方便。通常,使用信号的 POSIX 应用程序
处理程序必须在每个库函数之后检查 EINTR
返回它,以便再次尝试调用。程序员常常忘记
检查,这是常见的错误来源。

因此,您可以处理 EINTR 错误,顺便说一句,还有另一种选择,您可以使用 sigaction 建立一个信号处理程序,指定该处理程序应如何表现。使用SA_RESTART标志,从该处理程序返回将恢复原语;否则,从该处理程序返回将导致 EINTR。

请参阅中断原语

From http://www.gnu.org/

A signal can arrive and be handled while an I/O primitive such as open
or read is waiting for an I/O device. If the signal handler returns,
the system faces the question: what should happen next?

POSIX specifies one approach: make the primitive fail right away. The
error code for this kind of failure is EINTR. This is flexible, but
usually inconvenient. Typically, POSIX applications that use signal
handlers must check for EINTR after each library function that can
return it, in order to try the call again. Often programmers forget to
check, which is a common source of error.

So you can handle the EINTR error, there is another choice by the way, You can use sigaction to establish a signal handler specifying how that handler should behave. Using the SA_RESTART flag, return from that handler will resume a primitive; otherwise, return from that handler will cause EINTR.

see interrupted primitives

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文