When to check for EINTR and repeat the function call?

守給你的承諾、 提交于 2019-11-27 20:00:53
Yann Droneaud

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.

Maxim Egorushkin

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.

This link has a pretty good explanation.

http://www.ibm.com/developerworks/linux/library/l-reent.html

Cheers!

I had a similar problem when waiting for input from a named pipe with read().

I found an explanation and a useful macro for primitives in GNU libc documentation: TEMP_FAILURE_RETRY

Example:

TEMP_FAILURE_RETRY (read_return = read((int)example_fifo, buffer, (size_t)n));
if (read_return==-1){
    fprintf(stderr, "reader.c: read_fifo: read(): %s \n", strerror(errno));
    fflush(stderr);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!