When to Check for Eintr and Repeat the Function Call

When to check for EINTR and repeat the function call?

See sigaction : http://pubs.opengroup.org/onlinepubs/009695399/functions/sigaction.html

SA_RESTART
This flag affects the behavior of interruptible functions; that is, those
specified to fail with errno set to EINTR. If set, and a function specified
as interruptible is interrupted by this signal, the function shall restart
and shall not fail with EINTR unless otherwise specified. If the flag is not
set, interruptible functions interrupted by this signal shall fail with errno
set to EINTR.

By default, you have the SA_RESTART behavior, so you don't have to worry about EINTR, if you don't play with signals.

Checking if errno != EINTR: what does it mean?

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.

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).

Should we care about EINTR when using sigwait?

EINTR is only returned if the system call was interrupted by a signal handler. If all signals are blocked in the signal mask of the thread that's making the system call, then this can't happen.

System calls and EINTR error code

See http://man7.org/linux/man-pages/man7/signal.7.html -- start reading near the bottom where it talks about "Interruption of system calls and library functions..." This is a Linux man page, but the info is pretty generally applicable to any Unix/Posix/Linux-flavored system.

Is sem_timedwait with EINTR-check guaranteed to wait = the specified time?


Assuming the semaphore is not posted-to (i.e. timeout is expected), is
the while-loop guaranteed to exit at the time specified in ts (or
slightly later)? I.e. is it guaranteed that the while-loop will not
exit before the time specified in ts?

It depends on what you mean by "guaranteed", but the specifications for sem_timedwait do not provide for it to time out before the specified time has expired. It can fail sooner for other reasons, though, so in that sense no, it is not guaranteed that the example while loop will run for the full time specified.

In particular, even if all the arguments are valid and the call is not interrupted by a signal, sem_timedwait() is explicitly permitted to fail with EDEADLK to indicate that a deadlock was detected.



Related Topics



Leave a reply



Submit