TEMP_FAILURE_RETRY,__extension,MSG_NOSIGNAL
转自:http://blog.csdn.net/apengjiang/article/details/7056295
关于使用TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(expression) \
(__extension__\
({ long int __result;\
do __result = (long int)(expression);\
while(__result == -1L&& errno == EINTR);\
__result;})\
#endif
The GNU library provides a convenient way to retry a call after a temporary failure, with the macro TEMP_FAILURE_RETRY: — Macro: TEMP_FAILURE_RETRY (expression)
This macro evaluates expression once, and examines its value as type long int. If the value equals -1, that indicates a failure and errno should be set to show what kind of failure. If it fails and reports error code EINTR, TEMP_FAILURE_RETRY evaluates it again, and over and over until the result is not a temporary failure. The value returned by TEMP_FAILURE_RETRY is whatever value expression produced.
举个例子(etc.)TEMP_FAILURE_RETRY(::accept(sock, (struct sockaddr*)addr, &len));
它的功能: 不断地从套接口中接收客户端的地址知道成功为止返回客户端的可用套接口.
顺便写一个利用epoll技术来接收数据的routine
int accept(struct sockaddr_in *addr)
{
socklen_t len = sizeof(struct sockaddr_in);
bzero(addr, sizeof(struct sockaddr_in));
struct epoll_event ev;
int rc = epoll_wait(kdpfd, &ev, 1, 2000);//这里kdpfd is the library function epoll_create(
int size)的reture value.Here is called by epoll_create(1).You can man it.
if(1 == rc && (ev.events & EPOLLIN))
return TEMP_FAILURE_RETRY(::accept(sock, (struct sockaddr*)addr, &len));
return -1;
}
This macro evaluates expression once, and examines its value as type long int. If the value equals -1, that indicates a failure and errno should be set to show what kind of failure. If it fails and reports error code EINTR,TEMP_FAILURE_RETRY evaluates it again, and over and over until the result is not a temporary failure.
从代码里看到,当while为真时,会不断执行表达式,例如,我们可以在我们的套接字里不断读取数据
int ret = TEMP_FAILURE_RETRY(::recv(sock,buffer,size,MSG_NOSIGNAL));
这样程序得一直读到数据返回,
使用MSG_NOSIGNAL
linux下当连接断开,还发数据的时候,会向系统发送一个异常消息,如果不作处理,系统会出 BrokePipe,程序会退出。为此,函数的最后一个参数可以设MSG_NOSIGNAL,禁止向系统发送异常消息。
使用errno
表达式中函数作为不可重入函数,执行过程中,被信号中断,包含下面两种情况
Interruption of System Calls and Library Functions by Signal Handlers
-
If a signal handler is invoked while a system call or library function call is blocked, then either:
the call is automatically restarted after the signal handler returns; or the call fails with the error EINTR.
GCC uses the __extension__ attribute when using the -ansi flag to avoid
warnings in headers with GCC extensions. This is mostly used in glibc
with function declartions using long long.

浙公网安备 33010602011771号