김민기
김민기

Reputation: 43

When is EINTR error returned?

I saw the code(read, write system call) that handling EINTR error. I understood interrupt occur in two cases. First, it occur every time quantum for schedule. Second, it occur by signal. But everyone in stackoverflow said just signal case only.

So, My question is that EINTR is returned just by signal? or returned by something else?

Upvotes: 4

Views: 6120

Answers (2)

Heath Raftery
Heath Raftery

Reputation: 4169

Since you mention read and write, I'll assume you're referring to POSIX-compliant operating systems and answer for those. As jwdonahue has pointed out, it may be different for different functions and operating systems.

A return value of EINTR means that the function was interrupted by a signal before the function could finish its normal job. The signal itself may or may not have been caused by an interrupt. Let me elaborate, because the terms "interrupt", "signal" and "interrupted" are subtle.

A signal is simply a particular kind of inter-process communication. It allows the kernel to interrupt the execution of processes and therefore, processes to interrupt each other.

An interrupt, on the other hand, is a lower level, often hardware related phenomenon that originates in the processor. In POSIX environments an interrupt is often turned into a signal by the kernel and sent to relevant processes.

So EINTR means a signal was received. The signal caused the process to be "interrupted" (not to be confused with an interrupt). The signal may or may not have been caused by an underlying interrupt. For example, SIGSEGV and SIGBUS are caused by interrupts while SIGINT (confusingly enough) is caused by software - generally when a Ctrl-C is sent to the terminal.

Upvotes: 9

user8969424
user8969424

Reputation:

I think you are getting confused with interrupts, which is an asynchronous event, and EINTR which is just a error code.

Not all interrupted code result in errno being set to EINTR. errno is set to EINTR when some system calls are interrupted and the system is not in a position to resume the system call after interrupt handling. Developer may choose to retry the system call again by checking errno.

That said, any user defined function can set errno to EINTR too without any interrupts in the picture.

For eg:

int foo () {
    //do_some_stuff_here
    errno = EINTR;
    return -1;
}

is perfectly valid (may not be too useful, but valid).

Upvotes: 0

Related Questions