Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to check for EINTR and repeat the function call?

Tags:

linux

eintr

I am programming a user application for a embedded Linux system, and I am using the common functions such as open, close, read, ioctl, etc. for the devices. Now, I read about EINTR, indicates that the function was interrupted by a signal, but I am not sure about the implications. In all the example programs I have, sometimes it is done, e.g. ioctl(), sometimes it is not done, e.g. read(). So, I am a little bit confused.

When do I preferably check for EINTR and repeat the function call?

like image 800
stefangachter Avatar asked Feb 10 '11 16:02

stefangachter


2 Answers

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.

like image 58
Yann Droneaud Avatar answered Sep 28 '22 04:09

Yann Droneaud


Is your application event driven? (Meaning its main loop include select()/epoll_wait() call).

In an event driven application you can block all signals and only unblock them for the duration of pselect()/epoll_pwait() call. This way the rest of your code never have to deal with EINTR.

like image 38
Maxim Egorushkin Avatar answered Sep 28 '22 04:09

Maxim Egorushkin