开发者

interrupted system call error when writing to a pipe

开发者 https://www.devze.com 2023-03-03 15:18 出处:网络
In my user space Linux application, I have a thread which communicated to the main process through a pipe. Below is the code

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,


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消