写入管道时出现中断系统调用错误
在我的用户空间 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
write(2) 手册页提到:
我猜你只是幸运,到目前为止还没有发生这种情况。
如果您只是搜索“中断的系统调用”,您会发现此线程,它告诉您可以使用
siginterrupt()
自动重新启动write
调用。The write(2) man page mentions:
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 thewrite
call.来自 http://www.gnu.org/
因此,您可以处理 EINTR 错误,顺便说一句,还有另一种选择,您可以使用 sigaction 建立一个信号处理程序,指定该处理程序应如何表现。使用
SA_RESTART
标志,从该处理程序返回将恢复原语;否则,从该处理程序返回将导致 EINTR。请参阅中断原语
From http://www.gnu.org/
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